• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

AndroidWebSocket: a simple use for websocket in android

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

AndroidWebSocket

开源软件地址:

https://gitee.com/leohan1992/AndroidWebSocket

开源软件介绍:

AndroidWebSocket

Help to use webSocket in Android
based on "org.java-websocket:Java-WebSocket:1.3.4"

dependencies

Maven

<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

  1. write your own BroadcastReceiver extends xyz.leohan.websocketlib.WebSocketReceiver like this:
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);    }}
  1. write these in your AndroidManifest.xml:
 <service android:name="xyz.leohan.websocketlib.WebSocketService" /> <receiver android:name=".MyReceiver">    <intent-filter>        <action android:name="xyz.leohan.Broadcast.WebSocket" />     </intent-filter> </receiver>
  1. call init() :
 //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              }          } );
  1. call disconnect:

it will auto reconnect by error or server offline,but when you call disconnect,you need call connect again

   WebSocketAndroidClient.getInstance().disConnect();
  1. send message:
   WebSocketAndroidClient.getInstance().sendMsg(editText.getText().toString());

History

  • v1.1.4-beta1.update gradle to 4.12.update buildTool to 26.0.23.add isConnected function.
  • v1.1.3-beta
    1. add init function. add timeout time param.
  • v1.1.2-beta
    1. add connenct listener
    2. fix disconnect and reconnect bug
    3. throw NotYetConnectException when send msg

Contact me

if you have any trouble in use this lib ,you can send me an e-mail :[email protected]


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap