Android中使用WebSocket

Android中使用WebSocket,第1张

概述参考网址:https://blog.csdn.net/mr_lichao/article/details/106102004https://blog.csdn.net/huangyuan_xuan/article/details/109325021//导包:implementation"org.java-websocket:Java-WebSocket:1.4.0"privateWebSocketClientwebSocketClient;privatevoid

参考网址:https://blog.csdn.net/mr_lichao/article/details/106102004
https://blog.csdn.net/huangyuan_xuan/article/details/109325021

//导包:implementation "org.java-websocket:Java-WebSocket:1.4.0"private WebSocketClIEnt webSocketClIEnt;    private voID webSocket(){        //https://blog.csdn.net/huangyuan_xuan/article/details/109325021        //https://www.jianshu.com/p/ee5bdb999df6        new Thread(){            @OverrIDe            public voID run() {                super.run();                try{                    URI serverURI = URI.create("ws://192.168.1.1:8080?");                    webSocketClIEnt = new WebSocketClIEnt(serverURI) {                        //打开连接                        @OverrIDe                        public voID onopen(ServerHandshake handshakedata) {                            System.out.println("onopen:服务器状态:"+handshakedata.gethttpStatusMessage());                        }                        //服务端返回消息                        @OverrIDe                        public voID onMessage(String message) {                            System.out.println("onMessage:"+message); //得到返回的数据                        }                        //关闭连接                        @OverrIDe                        public voID onClose(int code, String reason, boolean remote) {                            System.out.println("onClose:"+code+reason+remote);                        }                        //出现异常                        @OverrIDe                        public voID one rror(Exception ex) {                            System.out.println("onError:"+ex.fillinStackTrace());                        }                    };                    webSocketClIEnt.connectBlocking();                    mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//开启心跳检测                }catch (Exception e){                    System.out.println("webSocket:"+e.fillinStackTrace());                }            }        }.start();    }    //******websocket心跳检测********    private String heartbeat = "123";//心跳    private static final long HEART_BEAT_RATE = 10 * 1000;//每隔10秒进行一次对长连接的心跳检测    private Handler mHandler = new Handler();    private Runnable heartBeatRunnable = new Runnable() {        @OverrIDe        public voID run() {            System.out.println("心跳包检测websocket连接状态");            if (webSocketClIEnt != null) {                if (webSocketClIEnt.isClosed()) {                    reconnectWs();                }else {                    //业务逻辑 这里如果服务端需要心跳包为了防止断开 需要不断发送消息给服务端                    webSocketClIEnt.send(heartbeat);                    System.out.println("客户端发送消息:"+heartbeat);                }            } else {                //如果clIEnt已为空,重新初始化连接                webSocketClIEnt = null;                webSocket();            }            //每隔一定的时间,对长连接进行一次心跳检测            mHandler.postDelayed(this, HEART_BEAT_RATE);        }    };    //开启重连    private voID reconnectWs() {        mHandler.removeCallbacks(heartBeatRunnable);        new Thread() {            @OverrIDe            public voID run() {                try {                    //connectBlocking多出一个等待 *** 作,会先连接再发送,否则未连接发送会报错                    webSocketClIEnt.reconnectBlocking();                } catch (Exception e) {                    System.out.println("reconnectWs:"+e.fillinStackTrace());                }            }        }.start();    }	@OverrIDe    protected voID onDestroy() {        super.onDestroy();        mHandler.removeCallbacks(heartBeatRunnable);        if (webSocketClIEnt!=null){            webSocketClIEnt.close();            webSocketClIEnt = null;        }    }

 

总结

以上是内存溢出为你收集整理的Android中使用WebSocket全部内容,希望文章能够帮你解决Android中使用WebSocket所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046658.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存