import { app } from "../tools/config";
import { ISendDelegate, IReceiveDelegate } from "./delegate";
import { initSender } from "./send";
import { initReceiver } from "./receive";
import { events, EventKind } from '../tools/event';
import gHandler = require('../../../../common/script/common/gHandler');
import { Msg } from "../proto/proto_msg";
class Client implements ISendDelegate, IReceiveDelegate {
private static _instance: Client;
private io: WebSocket;
private timer: number;
private mapHandler: Map<number, IResponseFunc>;
constructor() {
this.timer = 0;
this.io = undefined;
this.mapHandler = new Map();
initSender(this);
initReceiver(this);
}
public static getInstance(): Client {
this._instance || (this._instance = new Client());
return this._instance;
}
public init(): void {
this.io || this.initSocket();
}
/**
* 发送协议
* @param kind 协议名
* @param data 数据
*/
public sendMessage(kind: number, data: any): void {
if (this.io.readyState !== WebSocket.OPEN) {
return;
}
let bodyClass: any;
switch (kind) {
case 0:
bodyClass = Msg.heartBeat; //心跳
break;
case 1:
bodyClass = Msg.gameMsg; //测试登入
break;
default:
break;
}
const message = bodyClass.create(data);
// console.log("发送的数据:", message);
con