Android使用websocket

來源:互聯網
上載者:User

標籤:des   android   http   io   os   使用   ar   java   sp   

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

package com.fkapp.websocket;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 StringTAG= "MainActivity";public static StringwsUrl= "ws://ip:port/chat"; /* TODO: 運行時替換ip port */public WebSocketConnectionwsC= new WebSocketConnection();public Handler handler = new Handler(){@Overridepublic 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();}}@Overrideprotected 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" );    }    } );}@Overrideprotected void onDestroy(){super.onDestroy();if ( wsC.isConnected() ){wsC.disconnect();}}@Overridepublic 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);}@Overridepublic 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 ) );}}

進行通訊

Android使用websocket

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.