|
@@ -0,0 +1,80 @@
|
|
|
+var BaseClientWebsocket = function(addr,token,room,user,autoLogin){
|
|
|
+ this.seq = 1;
|
|
|
+ this.token = token;
|
|
|
+ this.addr = addr;
|
|
|
+ this.room = room;
|
|
|
+ this.user = user;
|
|
|
+ this.autoLogin = autoLogin;
|
|
|
+ this.client = null;
|
|
|
+};
|
|
|
+
|
|
|
+BaseClientWebsocket.prototype = {
|
|
|
+ init: function () {
|
|
|
+ var _self = this;
|
|
|
+ if(!window.WebSocket) {
|
|
|
+ console.log('您的设备不支持WebSocket');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.client = new WebSocket(this.addr);
|
|
|
+ this.client.onopen = function () {
|
|
|
+ if(_self.autoLogin) {
|
|
|
+ _self.login();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ this.client.onerror = function(){
|
|
|
+ console.log("onerror");
|
|
|
+ };
|
|
|
+ this.client.onclose = function(){
|
|
|
+ console.log("onclose");
|
|
|
+ _self.reconnect();
|
|
|
+ };
|
|
|
+ this.client.onmessage = function(data){
|
|
|
+
|
|
|
+ };
|
|
|
+ },
|
|
|
+ login: function(){
|
|
|
+ var msg = {
|
|
|
+ act: 'access',
|
|
|
+ op: 'login',
|
|
|
+ seq : this.seq++,
|
|
|
+ token: this.token
|
|
|
+ };
|
|
|
+ this.client.send(JSON.stringify(msg));
|
|
|
+ },
|
|
|
+ reconnect:function(){
|
|
|
+ var _self = this;
|
|
|
+ setTimeout(function(){
|
|
|
+ console.log("websocket重连中...");
|
|
|
+ _self.init();
|
|
|
+ },1000);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+var ShakeClientWebsocket = function(){
|
|
|
+ BaseClientWebsocket.apply(this,arguments);
|
|
|
+ this.init();
|
|
|
+ this.shake_bonus = function(msg){
|
|
|
+ this.client.send(msg)
|
|
|
+ }
|
|
|
+};
|
|
|
+ShakeClientWebsocket.prototype = new BaseClientWebsocket();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|