|
|
@@ -1,228 +1,234 @@
|
|
|
import WebSocket from 'ws';
|
|
|
|
|
|
export default class Msg {
|
|
|
- private static sendBufLen = 0;
|
|
|
- private static sendBuf = new ArrayBuffer(512);
|
|
|
- private static sendBufDV = new DataView(Msg.sendBuf, 0);
|
|
|
- static websocket;
|
|
|
- static isConnect: Boolean;
|
|
|
- static isConnecting: boolean;
|
|
|
- static websocketM;
|
|
|
- static isConnectM:Boolean;
|
|
|
- static isConnectingM:Boolean;
|
|
|
- static isInitiative: boolean = false;
|
|
|
- static handler;
|
|
|
- static account;
|
|
|
-
|
|
|
- static connect(url, inputAccount) {
|
|
|
- console.log("开始连接 url ============== ", url);
|
|
|
- if (Msg.isConnect) {
|
|
|
- console.log("msg connect 连接已存在");
|
|
|
- } else if (Msg.isConnecting) {
|
|
|
- console.log("msg connect 正在连接中");
|
|
|
- } else {
|
|
|
+ public sendBufLen = 0;
|
|
|
+ public sendBuf = new ArrayBuffer(512);
|
|
|
+ public sendBufDV = new DataView(this.sendBuf, 0);
|
|
|
+ public websocket;
|
|
|
+ public isConnect: Boolean;
|
|
|
+ public isConnecting: boolean;
|
|
|
+ public websocketM;
|
|
|
+ public isConnectM: Boolean;
|
|
|
+ public isConnectingM: Boolean;
|
|
|
+ public isInitiative: boolean = false;
|
|
|
+ public handler;
|
|
|
+ public account;
|
|
|
+
|
|
|
+ onOpen(evt) {
|
|
|
+ this.isConnecting = false;
|
|
|
+ if (this.websocket.readyState == 1 && !this.isConnect) {
|
|
|
+ this.isConnect = true;
|
|
|
+ console.log("连接成功");
|
|
|
+ } else {
|
|
|
+ console.log("连接服务器失败(" + this.websocket.readyState + ")")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ onClose(evt) {
|
|
|
+ this.isConnect = false;
|
|
|
+ this.isConnecting = false;
|
|
|
+ if (this.websocket) {
|
|
|
+ this.websocket.close();
|
|
|
+ }
|
|
|
+ if (this.isInitiative) this.isInitiative = false;
|
|
|
+ if (this.isConnectM) this.websocketM.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ onMessage(evt) {
|
|
|
+ this.isConnect = false;
|
|
|
+ this.isConnecting = false;
|
|
|
+ // console.log("receive message ",evt.data)
|
|
|
+ }
|
|
|
+
|
|
|
+ onError(evt) {
|
|
|
+ this.isConnect = false;
|
|
|
+ this.isConnecting = false;
|
|
|
+ if (this.websocket) {
|
|
|
+ this.websocket.close();
|
|
|
+ }
|
|
|
+ if (this.isConnectM) this.websocketM.close();
|
|
|
+ // PlatformManager.onLoginError();
|
|
|
+ // LoadingPanel.instance.tip = "连接服务器失败(" + Msg.websocket.readyState + ")";
|
|
|
+ }
|
|
|
+
|
|
|
+ connect(url, inputAccount) {
|
|
|
+ console.log("开始连接 url ============== ", url);
|
|
|
+ if (this.isConnect) {
|
|
|
+ console.log("msg connect 连接已存在");
|
|
|
+ } else if (this.isConnecting) {
|
|
|
+ console.log("msg connect 正在连接中");
|
|
|
+ } else {
|
|
|
console.log("msg connect 开始连接");
|
|
|
- Msg.isConnect = false;
|
|
|
- Msg.isConnecting = true;
|
|
|
- Msg.websocket = new WebSocket(url);
|
|
|
- Msg.websocket.binaryType = "arraybuffer";
|
|
|
- Msg.websocket.onopen = function (evt) { Msg.onOpen(evt) };
|
|
|
- Msg.websocket.onclose = function (evt) { Msg.onClose(evt) };
|
|
|
- Msg.websocket.onmessage = function (evt) { Msg.handler.onMessage(evt) };
|
|
|
- Msg.websocket.onerror = function (evt) { Msg.onError(evt) };
|
|
|
- Msg.account = inputAccount;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- static onOpen(evt) {
|
|
|
- Msg.isConnecting = false;
|
|
|
- if (Msg.websocket.readyState == 1 && !Msg.isConnect) {
|
|
|
- Msg.isConnect = true;
|
|
|
- console.log("连接成功");
|
|
|
- } else {
|
|
|
- console.log("连接服务器失败(" + Msg.websocket.readyState + ")")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- static onClose(evt) {
|
|
|
- Msg.isConnect = false;
|
|
|
- Msg.isConnecting = false;
|
|
|
- if (Msg.websocket) {
|
|
|
- Msg.websocket.close();
|
|
|
- }
|
|
|
- if (Msg.isInitiative) Msg.isInitiative = false;
|
|
|
- if (Msg.isConnectM) Msg.websocketM.close();
|
|
|
- }
|
|
|
-
|
|
|
- static onError(evt) {
|
|
|
- Msg.isConnect = false;
|
|
|
- Msg.isConnecting = false;
|
|
|
- if (Msg.websocket) {
|
|
|
- Msg.websocket.close();
|
|
|
- }
|
|
|
- if (Msg.isConnectM) Msg.websocketM.close();
|
|
|
- // PlatformManager.onLoginError();
|
|
|
- // LoadingPanel.instance.tip = "连接服务器失败(" + Msg.websocket.readyState + ")";
|
|
|
- }
|
|
|
-
|
|
|
- static connectM(url)
|
|
|
- {
|
|
|
- if (Msg.isConnectM) {
|
|
|
- console.log("msg connectM 连接已存在");
|
|
|
- } else if (Msg.isConnectingM) {
|
|
|
- console.log("msg connectM 正在连接中");
|
|
|
- } else {
|
|
|
- Msg.isConnectM = false;
|
|
|
- Msg.isConnectingM = true;
|
|
|
- Msg.websocketM = new WebSocket(url);
|
|
|
- Msg.websocketM.binaryType="arraybuffer";
|
|
|
- Msg.websocketM.onopen = function(evt) { Msg.onOpenM(evt) };
|
|
|
- Msg.websocketM.onclose = function(evt) { Msg.onCloseM(evt) };
|
|
|
- Msg.websocketM.onmessage = function(evt) { Msg.handler.onMessageM(evt) };
|
|
|
- Msg.websocketM.onerror = function(evt) { Msg.onErrorM(evt) };
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- static onOpenM(evt) {
|
|
|
- Msg.isConnectingM = false;
|
|
|
- if (Msg.websocketM.readyState == 1 && !Msg.isConnectM) {
|
|
|
- Msg.isConnectM = true;
|
|
|
- // Msg.CG_MIDDLE_LOGIN(account);
|
|
|
- } else {
|
|
|
- //trace("sopcket middle err:", websocketM.readyState);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- static onCloseM(evt)
|
|
|
- {
|
|
|
- Msg.isConnectM = false;
|
|
|
- Msg.isConnectingM = false;
|
|
|
- console.log('onCloseM>>>>>>>>>>>>>>>>>>>>>>>>>>')
|
|
|
- if (Msg.websocketM) {
|
|
|
- Msg.websocketM.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- static onErrorM(evt)
|
|
|
- {
|
|
|
- Msg.isConnectM = false;
|
|
|
-
|
|
|
- Msg.isConnectM = false;
|
|
|
- Msg.isConnectingM = false;
|
|
|
- console.log('onErrorM>>>>>>>>>>>>>>>>>>>>>>>>>>')
|
|
|
- if (Msg.websocketM) {
|
|
|
- Msg.websocketM.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- static writeDouble(n) {
|
|
|
- Msg.sendBufDV.setFloat64(Msg.sendBufLen, n);
|
|
|
- Msg.sendBufLen = Msg.sendBufLen + 8;
|
|
|
- }
|
|
|
-
|
|
|
- static writeInt(n) {
|
|
|
- Msg.sendBufDV.setInt32(Msg.sendBufLen, n);
|
|
|
- Msg.sendBufLen = Msg.sendBufLen + 4;
|
|
|
- }
|
|
|
-
|
|
|
- static writeUint(n) {
|
|
|
- Msg.sendBufDV.setUint32(Msg.sendBufLen, n);
|
|
|
- Msg.sendBufLen = Msg.sendBufLen + 4;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- static writeShort(n) {
|
|
|
- Msg.sendBufDV.setInt16(Msg.sendBufLen, n);
|
|
|
- Msg.sendBufLen = Msg.sendBufLen + 2;
|
|
|
- }
|
|
|
-
|
|
|
- static writeByte(n) {
|
|
|
- Msg.sendBufDV.setInt8(Msg.sendBufLen, n);
|
|
|
- Msg.sendBufLen = Msg.sendBufLen + 1;
|
|
|
- }
|
|
|
-
|
|
|
- static writeString(str) {
|
|
|
- var oldPos = Msg.sendBufLen
|
|
|
- Msg.writeShort(0)
|
|
|
- var totalByte = 0
|
|
|
- var strLen = str.length
|
|
|
- for (var i = 0; i < strLen; ++i) {
|
|
|
- var t = str.charCodeAt(i)
|
|
|
- if (t < 128) {
|
|
|
- totalByte = totalByte + 1
|
|
|
- Msg.writeByte(t)
|
|
|
- }
|
|
|
- else if (t < 2048) {
|
|
|
- totalByte = totalByte + 2
|
|
|
- Msg.writeByte(t >> 6 | 192)
|
|
|
- Msg.writeByte(t & 63 | 128)
|
|
|
- }
|
|
|
- else if (t < 65536) {
|
|
|
- totalByte = totalByte + 3
|
|
|
- Msg.writeByte(t >> 12 | 224)
|
|
|
- Msg.writeByte(t >> 6 & 63 | 128)
|
|
|
- Msg.writeByte(t & 63 | 128)
|
|
|
- }
|
|
|
- else {
|
|
|
- throw "error";
|
|
|
- }
|
|
|
- }
|
|
|
- Msg.sendBufDV.setInt16(oldPos, totalByte);
|
|
|
- }
|
|
|
-
|
|
|
- static send(packetID) {
|
|
|
- var ab = new Uint8Array(Msg.sendBufLen)
|
|
|
- for (var i = 0; i < Msg.sendBufLen; ++i) {
|
|
|
- ab[i] = Msg.sendBufDV.getInt8(i);
|
|
|
- }
|
|
|
-
|
|
|
- Msg.websocket.send(ab.buffer);
|
|
|
- }
|
|
|
-
|
|
|
- static close() {
|
|
|
- if (Msg.websocket) {
|
|
|
- Msg.websocket.close();
|
|
|
- }
|
|
|
- if (Msg.websocketM) {
|
|
|
- Msg.websocketM.close();
|
|
|
- }
|
|
|
- Msg.isConnect = false;
|
|
|
- Msg.isConnecting = false;
|
|
|
- Msg.isConnectM = false;
|
|
|
- Msg.isConnectingM = false;
|
|
|
- }
|
|
|
-
|
|
|
- static CG_ASK_LOGIN(account: string, timestamp: number, authkey: string, lang: string, region: string, ip: string, params: string)
|
|
|
- {
|
|
|
- console.log("发送登录消息",Msg.websocket.readyState,Msg.isConnect);
|
|
|
- if (Msg.isConnect != true || Msg.websocket.readyState != 1) return false;
|
|
|
- Msg.sendBufLen = 4;
|
|
|
- Msg.writeString(account);
|
|
|
- Msg.writeInt(timestamp);
|
|
|
- Msg.writeString(authkey);
|
|
|
- Msg.writeString(lang);
|
|
|
- Msg.writeString(region);
|
|
|
- Msg.writeString(ip);
|
|
|
- Msg.writeString(params);
|
|
|
- Msg.sendBufDV.setInt16(2, 62);
|
|
|
- Msg.sendBufDV.setInt16(0, Msg.sendBufLen - 4);
|
|
|
- Msg.send(62);
|
|
|
- console.log("发送完了断开连接");
|
|
|
- Msg.close(); // 主动断开连接
|
|
|
+ const _that = this
|
|
|
+ this.isConnect = false;
|
|
|
+ this.isConnecting = true;
|
|
|
+ this.websocket = new WebSocket(url);
|
|
|
+ this.websocket.binaryType = "arraybuffer";
|
|
|
+ this.websocket.onopen = function (evt) { _that.onOpen(evt) };
|
|
|
+ this.websocket.onclose = function (evt) { _that.onClose(evt) };
|
|
|
+ this.websocket.onmessage = function (evt) { _that.onMessage(evt) };
|
|
|
+ this.websocket.onerror = function (evt) { _that.onError(evt) };
|
|
|
+ this.account = inputAccount;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ connectM(url) {
|
|
|
+ if (this.isConnectM) {
|
|
|
+ console.log("msg connectM 连接已存在");
|
|
|
+ } else if (this.isConnectingM) {
|
|
|
+ console.log("msg connectM 正在连接中");
|
|
|
+ } else {
|
|
|
+ const _that = this
|
|
|
+ this.isConnectM = false;
|
|
|
+ this.isConnectingM = true;
|
|
|
+ this.websocketM = new WebSocket(url);
|
|
|
+ this.websocketM.binaryType = "arraybuffer";
|
|
|
+ this.websocketM.onopen = function (evt) { _that.onOpenM(evt) };
|
|
|
+ this.websocketM.onclose = function (evt) { _that.onCloseM(evt) };
|
|
|
+ this.websocketM.onmessage = function (evt) { _that.handler.onMessageM(evt) };
|
|
|
+ this.websocketM.onerror = function (evt) { _that.onErrorM(evt) };
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ onOpenM(evt) {
|
|
|
+ this.isConnectingM = false;
|
|
|
+ if (this.websocketM.readyState == 1 && !this.isConnectM) {
|
|
|
+ this.isConnectM = true;
|
|
|
+ // Msg.CG_MIDDLE_LOGIN(account);
|
|
|
+ } else {
|
|
|
+ //trace("sopcket middle err:", websocketM.readyState);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ onCloseM(evt) {
|
|
|
+ this.isConnectM = false;
|
|
|
+ this.isConnectingM = false;
|
|
|
+ console.log('onCloseM>>>>>>>>>>>>>>>>>>>>>>>>>>')
|
|
|
+ if (this.websocketM) {
|
|
|
+ this.websocketM.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ onErrorM(evt) {
|
|
|
+ this.isConnectM = false;
|
|
|
+
|
|
|
+ this.isConnectM = false;
|
|
|
+ this.isConnectingM = false;
|
|
|
+ console.log('onErrorM>>>>>>>>>>>>>>>>>>>>>>>>>>')
|
|
|
+ if (this.websocketM) {
|
|
|
+ this.websocketM.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ writeDouble(n) {
|
|
|
+ this.sendBufDV.setFloat64(this.sendBufLen, n);
|
|
|
+ this.sendBufLen = this.sendBufLen + 8;
|
|
|
+ }
|
|
|
+
|
|
|
+ writeInt(n) {
|
|
|
+ this.sendBufDV.setInt32(this.sendBufLen, n);
|
|
|
+ this.sendBufLen = this.sendBufLen + 4;
|
|
|
+ }
|
|
|
+
|
|
|
+ writeUint(n) {
|
|
|
+ this.sendBufDV.setUint32(this.sendBufLen, n);
|
|
|
+ this.sendBufLen = this.sendBufLen + 4;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ writeShort(n) {
|
|
|
+ this.sendBufDV.setInt16(this.sendBufLen, n);
|
|
|
+ this.sendBufLen = this.sendBufLen + 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ writeByte(n) {
|
|
|
+ this.sendBufDV.setInt8(this.sendBufLen, n);
|
|
|
+ this.sendBufLen = this.sendBufLen + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ writeString(str) {
|
|
|
+ var oldPos = this.sendBufLen
|
|
|
+ this.writeShort(0)
|
|
|
+ var totalByte = 0
|
|
|
+ var strLen = str.length
|
|
|
+ for (var i = 0; i < strLen; ++i) {
|
|
|
+ var t = str.charCodeAt(i)
|
|
|
+ if (t < 128) {
|
|
|
+ totalByte = totalByte + 1
|
|
|
+ this.writeByte(t)
|
|
|
+ }
|
|
|
+ else if (t < 2048) {
|
|
|
+ totalByte = totalByte + 2
|
|
|
+ this.writeByte(t >> 6 | 192)
|
|
|
+ this.writeByte(t & 63 | 128)
|
|
|
+ }
|
|
|
+ else if (t < 65536) {
|
|
|
+ totalByte = totalByte + 3
|
|
|
+ this.writeByte(t >> 12 | 224)
|
|
|
+ this.writeByte(t >> 6 & 63 | 128)
|
|
|
+ this.writeByte(t & 63 | 128)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ throw "error";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.sendBufDV.setInt16(oldPos, totalByte);
|
|
|
+ }
|
|
|
+
|
|
|
+ send(packetID) {
|
|
|
+ var ab = new Uint8Array(this.sendBufLen)
|
|
|
+ for (var i = 0; i < this.sendBufLen; ++i) {
|
|
|
+ ab[i] = this.sendBufDV.getInt8(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.websocket.send(ab.buffer);
|
|
|
+ }
|
|
|
+
|
|
|
+ close() {
|
|
|
+ if (this.websocket) {
|
|
|
+ this.websocket.close();
|
|
|
+ }
|
|
|
+ if (this.websocketM) {
|
|
|
+ this.websocketM.close();
|
|
|
+ }
|
|
|
+ this.isConnect = false;
|
|
|
+ this.isConnecting = false;
|
|
|
+ this.isConnectM = false;
|
|
|
+ this.isConnectingM = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ CG_ASK_LOGIN(account: string, timestamp: number, authkey: string, lang: string, region: string, ip: string, params: string, server_id: number) {
|
|
|
+ console.log("发送登录消息", this.websocket.readyState, this.isConnect);
|
|
|
+ if (this.isConnect != true || this.websocket.readyState != 1) return false;
|
|
|
+ this.sendBufLen = 4;
|
|
|
+ this.writeString(account);
|
|
|
+ this.writeInt(timestamp);
|
|
|
+ this.writeString(authkey);
|
|
|
+ this.writeString(lang);
|
|
|
+ this.writeString(region);
|
|
|
+ this.writeString(ip);
|
|
|
+ this.writeString(params);
|
|
|
+ this.writeInt(server_id);
|
|
|
+ this.sendBufDV.setInt16(2, 62);
|
|
|
+ this.sendBufDV.setInt16(0, this.sendBufLen - 4);
|
|
|
+ this.send(62);
|
|
|
+ console.log("发送完了断开连接");
|
|
|
+ this.close(); // 主动断开连接
|
|
|
return true;
|
|
|
- }
|
|
|
-
|
|
|
- static CG_TEST_PROTO(account:string,param:string) {
|
|
|
- console.log("发送CDK道具消息",Msg.websocket.readyState,Msg.isConnect);
|
|
|
- if (Msg.isConnect != true || Msg.websocket.readyState != 1) return false;
|
|
|
- Msg.sendBufLen = 4;
|
|
|
- Msg.writeString(account)
|
|
|
- Msg.writeString(param)
|
|
|
- Msg.sendBufDV.setInt16(2, 1259);
|
|
|
- Msg.sendBufDV.setInt16(0, Msg.sendBufLen - 4);
|
|
|
- Msg.send(1259);
|
|
|
- console.log("发送完了断开连接");
|
|
|
- Msg.close(); // 主动断开连接
|
|
|
+ }
|
|
|
+
|
|
|
+ CG_TEST_PROTO(account: string, param: string,server_id = 1) {
|
|
|
+ console.log("发送CDK道具消息", this.websocket.readyState, this.isConnect);
|
|
|
+ if (this.isConnect != true || this.websocket.readyState != 1) return false;
|
|
|
+ this.sendBufLen = 4;
|
|
|
+ this.writeString(account)
|
|
|
+ this.writeString(param)
|
|
|
+ this.writeInt(server_id);
|
|
|
+ this.sendBufDV.setInt16(2, 1259);
|
|
|
+ this.sendBufDV.setInt16(0, this.sendBufLen - 4);
|
|
|
+ this.send(1259);
|
|
|
+ console.log("发送完了断开连接");
|
|
|
+ this.close(); // 主动断开连接
|
|
|
return true;
|
|
|
- }
|
|
|
+ }
|
|
|
}
|