Android使用websocket

jopen 10年前發布 | 111K 次閱讀 WebSocket 開發 WebSocket

使用library: https://github.com/tavendo/AutobahnAndroid

import com.fkapp.websocket.R;

import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    private final String      TAG = "MainActivity";
    public static String      wsUrl   = "ws://ip:port/chat"; /* TODO: 運行時替換ip port */
    public WebSocketConnection wsC = new WebSocketConnection();

    public Handler handler = new Handler()
    {
        @Override
        public void handleMessage( Message msg )
        {
            super.handleMessage( msg );
            if ( msg.what == 0 )
            {
            }
        }
    };

    public void toastLog( String s )
    {
        Toast.makeText( this, s, Toast.LENGTH_SHORT ).show();
    }


    private void wsStart()
    {
        try {
            wsC.connect( wsUrl, new WebSocketHandler()
                     {
                         @Override
                         public void onOpen()
                         {
                             toastLog( "Status: Connected to " + wsUrl );
                             wsC.sendTextMessage( "Hello, world!" );
                         }

                         @Override
                         public void onTextMessage( String payload )
                         {
                             toastLog( "Got echo: " + payload );
                         }

                         @Override
                         public void onClose( int code, String reason )
                         {
                             toastLog( "Connection lost." );
                         }
                     } );
        } catch ( WebSocketException e ) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        wsStart();

        Button wsSend = (Button) findViewById( R.id.wsSend );
        wsSend.setOnClickListener( new View.OnClickListener()
                        {
                            @Override
                            public void onClick( View v )
                            {
                                wsC.sendTextMessage( "ooxx" );
                            }
                        } );
    }


    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if ( wsC.isConnected() )
        {
            wsC.disconnect();
        }
    }


    @Override
    public boolean onCreateOptionsMenu( Menu menu )
    {
        /* Inflate the menu; this adds items to the action bar if it is present. */
        getMenuInflater().inflate( R.menu.main, menu );
        return(true);
    }


    @Override
    public boolean onOptionsItemSelected( MenuItem item )
    {
        /*
         * Handle action bar item clicks here. The action bar will
         * automatically handle clicks on the Home/Up button, so long
         * as you specify a parent activity in AndroidManifest.xml.
         */
        int id = item.getItemId();
        if ( id == R.id.action_settings )
        {
            return(true);
        }
        return(super.onOptionsItemSelected( item ) );
    }
}

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!