在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:AndroidWebSocket开源软件地址:https://gitee.com/leohan1992/AndroidWebSocket开源软件介绍:AndroidWebSocketHelp to use webSocket in Android dependenciesMaven <dependency> <groupId>xyz.leohan</groupId> <artifactId>AndroidWebSocket</artifactId> <version>1.1.3-beta</version> <type>pom</type></dependency> Gradle compile 'xyz.leohan:AndroidWebSocket:1.1.3-beta' How to Use
public class MyReceiver extends WebSocketReceiver { @Override public void onMessage(String msg) { //Here is message from webSocket //you can deal it with eventBus、RxBus、Notification and so on Log.i("webSocket", msg); }}
<service android:name="xyz.leohan.websocketlib.WebSocketService" /> <receiver android:name=".MyReceiver"> <intent-filter> <action android:name="xyz.leohan.Broadcast.WebSocket" /> </intent-filter> </receiver>
//do this in your BaseApplication or MainActivity //Make sure you called this only once //WebSocketAndroidClient.init(this,"ws://192.168.1.108:9898"); WebSocketAndroidClient.init(this,"ws://192.168.1.108:9898",3600); WebSocketAndroidClient.getInstance().connect(new WebSocketAndroidClient.onWebSocketOpenListener() { @Override public void onOpen(ServerHandshake handshakedata) { //TODO connected } } );
it will auto reconnect by error or server offline,but when you call disconnect,you need call connect again WebSocketAndroidClient.getInstance().disConnect();
WebSocketAndroidClient.getInstance().sendMsg(editText.getText().toString()); History
Contact meif you have any trouble in use this lib ,you can send me an e-mail :[email protected] |
请发表评论