From c5adcda0317e652de3fb87232389e494a41e8adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=91=E6=98=AFleo?= <907600065@qq.com> Date: Sun, 29 Jul 2018 23:18:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0ws=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 8196 bytes Websocket/assets/Scene/Websocket.fire | 443 ++++++++++++++++++++------ Websocket/assets/Script/HelloWorld.js | 33 +- Websocket/assets/Script/WsNet.js | 138 ++++++++ Websocket/assets/Script/WsNet.js.meta | 9 + Websocket/server/server.js | 2 - 6 files changed, 522 insertions(+), 103 deletions(-) create mode 100644 Websocket/assets/Script/WsNet.js create mode 100644 Websocket/assets/Script/WsNet.js.meta diff --git a/.DS_Store b/.DS_Store index 2a764cae74b5abd6a79e2db6993f89f537aaad0a..5841d7e36628e135ef0d0e6473151a9d44e53084 100644 GIT binary patch literal 8196 zcmeHMO>Y}T7=9-W?b=P6B@J~zfL26Em1-foUjZS&I0?bxBPN@s2}yC+-ifoyddIBS zanlgw3%`M{3lb-O06(BdE?hZs;0J(&IEQCuH_3YA_P_yE%8oVj%+Aa+@4U~<&S=&| z#L8W}NHk4E8lpt~B9b9N*2{P*j6@}qKn6aMO*=GCoLVGKwN49C1yTi41yTi41yTi` z1_f|t%b-2x+z)HhG*uu~;7lqY_Xh(}VkyFYh-&LVqOJhIQ536yJnjLCV?n0V3Y#C09+wG9AvXx!q$1PKY^`bZKZ~BkH*l}SA7dAL(8q<{xGRw zoR`6v7BFY?^bzniXlD)9REj~P~IT1Ly%^y&Tm z>xDP+M)B6`2YF+EcD9f=Zq61D4l-Kq)#6)~`>nl)`(GS<`7InoVA1JA6XEl5`W5*$HBX?n2Hwrb1w1c92g_2c~dfI)*TwG9imMZs(2bHw!01-njjrgdF0k z)7XvH3_D&kzw7$Zs>x(8j*VZM(4U>WJoVg_TrM~LeC~zG7hkIBm+Mwzr|HZ2pA)X| z%Wjv~^ob5uRHY%js?T?^de#E|ATXVWsG3X=mbv}eNzuw)8*Qde3RI_yKIh&>(|7Ep zt*s6Z)})1oFI=}OI!@pSZ#`DKzNxGSyjAhG1T4xCEVX5`&MNmiz*GFSb<6iHugS}M z_gt&a-6gLqfUroLynBuxl*OZn3vn ziB;G#Gua*XDcfLn*u%)tJM;39xe!?kIAp35Z;Gzol!iKB0$Nl@1*aOeBtcDVO5Wf$ zCO71P_h!&&`V>>(T$rDh_x}Z~jPyG|Oq?{EDsV;=V59Tqd>PIj?bJ!Xr3m{Wq6XoA{}7O#|LOU!R^Hi8{spXvW6J;l delta 140 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{MGjUEV6q~50D9Q?w2aEYJ1T(mL=Hw?Q { - console.log('test_pust', data); + // this.net = net; + // this.net.connect(); + // this.net.addHandler('test_pust', (data) => { + // console.log('test_pust', data); + // }); + // this.net.send('test_pust', 'hello'); + // emitter.on('test_1', this.onTest_1, this); + // emitter.on('test_2', this.onTest_2); + // emitter.emit('test_1', 1, 1, 1); + // emitter.emit('test_2', 1, 1, 1); + // emitter.off('test_1', this.onTest_1); + + this.initWsNet(); + }, + + initWsNet() { + wsNet.init('ws://192.168.2.176:8181', (result) => { + console.log('result=', result); }); - this.net.send('test_pust', 'hello'); - emitter.on('test_1', this.onTest_1, this); - emitter.on('test_2', this.onTest_2); - emitter.emit('test_1', 1, 1, 1); - emitter.emit('test_2', 1, 1, 1); - emitter.off('test_1', this.onTest_1); + }, + + onEventClick_close() { + wsNet.close(); }, diff --git a/Websocket/assets/Script/WsNet.js b/Websocket/assets/Script/WsNet.js new file mode 100644 index 0000000..48f735f --- /dev/null +++ b/Websocket/assets/Script/WsNet.js @@ -0,0 +1,138 @@ +const connectType = { + ok: 'open', + err: 'close' +} +class WsNet { + + constructor() { + this.ws = null; + this.wsUrl = ''; + this.wsTimeout = 2000; + this.lockReconnect = false; + //心跳检测 + this.checkTimeout = 60000;//60秒 + this.timeoutObj = null; + this.serverTimeoutObj = null; + + } + + /** + * 重置检查 + */ + resetTimeout() { + clearTimeout(this.timeoutObj); + clearTimeout(this.serverTimeoutObj); + return this; + } + + /** + * 开始检查 + */ + startTimeout() { + var self = this; + this.timeoutObj = setTimeout(function () { + //这里发送一个心跳,后端收到后,返回一个心跳消息, + //onmessage拿到返回的心跳就说明连接正常 + self.ws.send("HeartBeat"); + //如果超过一定时间还没重置,说明后端主动断开了 + self.serverTimeoutObj = setTimeout(() => { + //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次 + self.ws.close(); + }, self.checkTimeout); + }, this.checkTimeout); + } + + init(wsUrl, fn) { + this.wsUrl = wsUrl; + this.fn = fn; + this.lockReconnect = false; + //是否关闭连接 + this.isCloseWs = false; + this.createWebSocket(wsUrl); + this.isSuccess=false; + setTimeout(() => { + if(this.isSuccess) return; + this.close(); + if (this.fn) this.fn(connectType.err); + }, 10000); + } + + /** + * 关闭 + */ + isClose() { + return this.isCloseWs; + } + + /** + * 创建一个socket + * @param {*} url + */ + createWebSocket(url) { + try { + this.ws = new WebSocket(url); + this.initEventHandle(); + } catch (e) { + this.reconnect(url); + } + } + + /** + * 初始化事件 + */ + initEventHandle() { + this.ws.onclose = (evnt) => { + console.log('websocket服务关闭了',evnt); + if(this.isClose()) return; + if (this.fn) this.fn(connectType.err); + this.reconnect(this.wsUrl); + }; + this.ws.onerror = (evnt) => { + console.log('websocket服务出错了',evnt); + if(this.isClose()) return; + if (this.fn) this.fn(connectType.err); + this.reconnect(this.wsUrl); + }; + this.ws.onopen = (evnt) => { + console.log('websocket 连接成功'); + this.isSuccess=true; + //心跳检测重置 + this.resetTimeout().startTimeout(); + if (this.fn) this.fn(connectType.ok); + }; + this.ws.onmessage = (event) => { + console.log('event=', event); + //如果获取到消息,心跳检测重置 + this.resetTimeout().startTimeout(); + + } + } + + /** + * 重连 + * @param {*} url + */ + reconnect(url) { + console.log('事件重连'); + if (this.lockReconnect) return; + this.lockReconnect = true; + //没连接上会一直重连,设置延迟避免请求过多 + setTimeout(() => { + this.createWebSocket(url); + this.lockReconnect = false; + }, this.wsTimeout); + } + + /** + * 关闭ws + */ + close() { + this.isCloseWs=true; + this.ws.close(); + } + +} + +module.exports = new WsNet(); + + diff --git a/Websocket/assets/Script/WsNet.js.meta b/Websocket/assets/Script/WsNet.js.meta new file mode 100644 index 0000000..ece6b0e --- /dev/null +++ b/Websocket/assets/Script/WsNet.js.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.0.5", + "uuid": "97b4fb72-fa2c-46b8-bee5-f0431c8fc549", + "isPlugin": false, + "loadPluginInWeb": true, + "loadPluginInNative": true, + "loadPluginInEditor": false, + "subMetas": {} +} \ No newline at end of file diff --git a/Websocket/server/server.js b/Websocket/server/server.js index f1d1044..ec6f32d 100644 --- a/Websocket/server/server.js +++ b/Websocket/server/server.js @@ -8,6 +8,4 @@ wss.on('connection', function (ws) { }); }); -console.log('consol1'); console.log("websocket连接完毕") -console.log('a'); \ No newline at end of file