From 60aa4a21f189c31898c622b362b62878ec3ff455 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Tue, 14 Jul 2015 16:25:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Donopen=E5=92=8Conmessage?= =?UTF-8?q?=E4=B8=8D=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/websocket.min.js | 2 +- lib/websocket.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/build/websocket.min.js b/build/websocket.min.js index a6be29c..6d9e549 100644 --- a/build/websocket.min.js +++ b/build/websocket.min.js @@ -1 +1 @@ -(function(a,b){var c=b(a);if(typeof define==="function"&&define.amd){define([],b)}else if(typeof module!=="undefined"&&module.exports){module.exports=b()}else{a.ws=b()}})(this,function(a,b){if(!("WebSocket"in window))return;var c={},d,e=document.createElement("div"),f={automaticOpen:true,reconnectDecay:1.5},g=function(){},h={CONNECTING:WebSocket.CONNECTING,OPEN:WebSocket.OPEN,CLOSING:WebSocket.CLOSING,CLOSED:WebSocket.CLOSED};function i(a,b,c){var d=this;this.url=a;this.readyState=WebSocket.CONNECTING;this.protocol=b?b:null;if(!c){c={}}for(var g in f){if(typeof c[g]!=="undefined")this[g]=c[g];else this[g]=f[g]}for(var i in h)this[i]=h[i];e.addEventListener("open",function(a){d.onopen(a)});e.addEventListener("close",function(a){d.onclose(a)});e.addEventListener("connecting",function(a){d.onconnecting(a)});e.addEventListener("message",function(a){d.onmessage(a)});e.addEventListener("error",function(a){d.onerror(a)});this.addEventListener=e.addEventListener.bind(e);this.removeEventListener=e.removeEventListener.bind(e);this.dispatchEvent=e.dispatchEvent.bind(e);if(this.automaticOpen===true)this.open();return this}function j(a,b){var c=document.createEvent("CustomEvent");c.initCustomEvent(a,false,false,b);return c}i.prototype.onconnecting=g;i.prototype.onerror=g;i.prototype.send=function(a){if(this.wsocket)this.wsocket.send(a);else{throw"INVALID_STATE_ERR : Pausing to reconnect websocket"}};i.prototype.close=function(a,b){if(typeof a==="undefined")a=1e3;if(this.wsocket)this.wsocket.close(a,b)};i.prototype.open=function(){var a=this;d=new WebSocket(this.url,this.protocol||[]);e.dispatchEvent(j("connecting"));d.onopen=function(b){a.protocol=i.protocol;a.readyState=WebSocket.OPEN;var c=j("open");e.dispatchEvent(c)};d.onclose=function(b){a.readyState=WebSocket.CLOSED;var c=j("connecting");c.code=b.code;c.reason=b.reason;c.wasClean=b.wasClean;e.dispatchEvent(c);e.dispatchEvent(j("close"))};d.onmessage=function(a){var b=j("message");b.data=a.data;e.dispatchEvent(b)};d.onerror=function(a){var b=j("error");e.dispatchEvent(b)};this.wsocket=d;return this};return i}); \ No newline at end of file +(function(a,b){var c=b(a);if(typeof define==="function"&&define.amd){define([],b)}else if(typeof module!=="undefined"&&module.exports){module.exports=b()}else{a.ws=b()}})(this,function(a,b){if(!("WebSocket"in window))return;var c={},d,e=document.createElement("div"),f={automaticOpen:true,reconnectDecay:1.5},g=function(){},h={CONNECTING:WebSocket.CONNECTING,OPEN:WebSocket.OPEN,CLOSING:WebSocket.CLOSING,CLOSED:WebSocket.CLOSED};function i(a,b,c){var d=this;this.url=a;this.readyState=WebSocket.CONNECTING;this.protocol=b?b:null;if(!c){c={}}for(var g in f){if(typeof c[g]!=="undefined")this[g]=c[g];else this[g]=f[g]}for(var i in h)this[i]=h[i];e.addEventListener("open",function(a){d.onopen(a)});e.addEventListener("close",function(a){d.onclose(a)});e.addEventListener("connecting",function(a){d.onconnecting(a)});e.addEventListener("message",function(a){d.onmessage(a)});e.addEventListener("error",function(a){d.onerror(a)});this.addEventListener=e.addEventListener.bind(e);this.removeEventListener=e.removeEventListener.bind(e);this.dispatchEvent=e.dispatchEvent.bind(e);if(this.automaticOpen===true)this.open();return this}function j(a,b){var c=document.createEvent("CustomEvent");c.initCustomEvent(a,false,false,b);return c}i.prototype.onconnecting=g;i.prototype.onerror=g;i.prototype.onopen=g;i.prototype.onmessage=g;i.prototype.send=function(a){if(this.wsocket)this.wsocket.send(a);else{throw"INVALID_STATE_ERR : Pausing to reconnect websocket"}};i.prototype.close=function(a,b){if(typeof a==="undefined")a=1e3;if(this.wsocket)this.wsocket.close(a,b)};i.prototype.open=function(){var a=this;d=new WebSocket(this.url,this.protocol||[]);e.dispatchEvent(j("connecting"));d.onopen=function(b){a.protocol=i.protocol;a.readyState=WebSocket.OPEN;var c=j("open");e.dispatchEvent(c)};d.onclose=function(b){a.readyState=WebSocket.CLOSED;var c=j("connecting");c.code=b.code;c.reason=b.reason;c.wasClean=b.wasClean;e.dispatchEvent(c);e.dispatchEvent(j("close"))};d.onmessage=function(a){var b=j("message");b.data=a.data;e.dispatchEvent(b)};d.onerror=function(a){var b=j("error");e.dispatchEvent(b)};this.wsocket=d;return this};return i}); \ No newline at end of file diff --git a/lib/websocket.js b/lib/websocket.js index f59cbf8..df0d923 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -94,6 +94,8 @@ ws.prototype.onconnecting = func; ws.prototype.onerror = func; + ws.prototype.onopen = func; + ws.prototype.onmessage = func; /** * [send 发送 websocket 消息]