123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- // @/utils/websocket.js
- // import { isJSON } from "@/utils/utils"
- class WebSocketClass {
- constructor(url) {
- this.lockReconnect = false; // 是否开始重连
- this.wsUrl = ""; // ws 地址
- this.globalCallback = null; // 回调方法
- this.userClose = false; // 是否主动关闭
- this.createWebSocket(url);
- console.log('================================================================== ')
- }
- createWebSocket(url) {
- console.log("WebSocket --- createWebSocket " , );
- // #ifdef H5
- if (typeof(WebSocket) === 'undefined') {
- this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
- return false
- }
- // #endif
- // #ifdef APP-PLUS
- if (typeof(uni.connectSocket) === 'undefined') {
- this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
- return false
- }
- // #endif
- this.wsUrl = url;
- try {
- // 创建一个this.ws对象【发送、接收、关闭socket都由这个对象操作】
- // #ifdef H5
- this.ws = new WebSocket(this.wsUrl);
- console.log("WebSocket初始化H5 ---------" , );
- this.initEventHandle();
- // #endif
- // #ifdef APP-PLUS
- this.ws = uni.connectSocket({
- url: this.wsUrl,
- success: data => {
- console.log("WebSocket初始化APP ---------" , );
- this.initEventHandle();
- }
- });
- // #endif
- } catch (e) {
- this.reconnect(url);
- }
- }
- // 初始化
- initEventHandle() {
- /**
- * 监听WebSocket连接打开成功
- */
- // #ifdef H5
- this.ws.onopen = () => {
- console.log("WebSocket连接打开H5" , event);
- };
- // this.ws.onopen = (event) => {
- // console.log("WebSocket连接打开H5" , event);
- // // this.globalCallback({text:'WebSocket连接打开H5'})
- // };
- // #endif
- // #ifdef APP-PLUS
- this.ws.onOpen(res => {
- console.log('WebSocket连接打开APP' , res);
- });
- // #endif
- // /**
- // * 连接关闭后的回调函数
- // */
- // #ifdef H5
- this.ws.onclose = (event) => {
- console.log('this.userClose = H5' , this.userClose)
- if (!this.userClose) {
- // this.reconnect(this.wsUrl); //重连
- }
- };
- // #endif
- // #ifdef APP-PLUS
- this.ws.onClose(() => {
- console.log('this.userClose = APP' , this.userClose)
- if (!this.userClose) {
- this.reconnect(this.wsUrl); //重连
- }
- });
- // #endif
- /**
- * 报错时的回调函数
- */
- // #ifdef H5
- this.ws.onerror = (event) => {
- if (!this.userClose) {
- console.log('WebSocket 重连 H5' );
- this.reconnect(this.wsUrl); //重连
- }
- };
- // #endif
- // #ifdef APP-PLUS
- this.ws.onError(() => {
- if (!this.userClose) {
- console.log('WebSocket 重连 APP' );
- this.reconnect(this.wsUrl); //重连
- }
- });
- // #endif
- /**
- * 收到服务器数据后的回调函数
- */
- // #ifdef H5
- this.ws.onmessage = (event) => {
- console.log('onmessage = H5', event)
- // if(isJSON(event.data)) {
- // const jsonobject = JSON.parse(event.data)
- // this.globalCallback(jsonobject)
- // }else {
- // this.globalCallback(event.data)
- // }
- };
- // #endif
- // #ifdef APP-PLUS
- this.ws.onMessage(event => {
- console.log('onmessage = APP-PLUS', event)
- // if(isJSON(event.data)) {
- // const jsonobject = JSON.parse(event.data)
- // this.globalCallback(jsonobject)
- // }else {
- // this.globalCallback(event.data)
- // }
- });
- // #endif
- }
- // 关闭ws连接回调
- reconnect(url) {
- if (this.lockReconnect) return;
- this.ws.close();
- this.lockReconnect = true; // 关闭重连,没连接上会一直重连,设置延迟避免请求过多
- setTimeout(() => {
- this.createWebSocket(url);
- this.lockReconnect = false;
- }, 1000);
- }
- // 发送信息方法
- webSocketSendMsg(msg) {
- this.ws && this.ws.send({
- data: msg,
- success() {
- console.log("消息发送成功");
- },
- fail(err) {
- console.log("关闭失败", err)
- }
- });
- }
- // 获取ws返回的数据方法
- getWebSocketMsg(callback) {
- this.globalCallback = callback
- }
- // 关闭ws方法
- closeSocket() {
- if (this.ws) {
- this.userClose = true;
- this.ws.close({
- success(res) {
- console.log("关闭成功", res)
- },
- fail(err) {
- console.log("关闭失败", err)
- }
- });
- }
- }
- writeToScreen(massage) {
- console.log(massage);
- }
- }
- export default WebSocketClass;
|