/** * Stalk-JavaScript, Node.js client. Supported express.js framework. * Support by@ nattapon.r@live.com * * Ahoo Studio.co.th */ import * as EventEmitter from "events"; import { IServerImp } from "../Base/IServerImp"; import { HttpStatusCode } from "../utils/httpStatusCode"; import { Authen } from "../utils/tokenDecode"; import { IPomeloResponse, IPomelo, ServerParam } from "../utils/PomeloUtils"; const Pomelo = require("../pomelo/nodeWSClient"); const Config = require(dirname + "/stalk_config.json"); /** * @deprecated Use es6 Map instead. */ export interface IDictionary { [k: string]: any; } export class ServerImplement implements IServerImp { private static Instance: ServerImplement; public static getInstance(): ServerImplement { if (this.Instance === null || this.Instance === undefined) { this.Instance = new ServerImplement(); } return this.Instance; } static connectionProblemString: string = "Server connection is unstable."; socket: IPomelo; public getSocket() { if (this.socket !== null) { return this.socket; } else { throw new Error("No socket instance!"); } } host: string; port: number | string; _isConnected = false; _isLogedin = false; connect = this.connectServer; constructor() { console.log("serv imp. constructor"); } public dispose() { console.warn("dispose socket client."); this.disConnect(); delete ServerImplement.Instance; } public disConnect(callBack?: Function) { let self = this; if (!!self.socket) { self.socket.removeAllListeners(); self.socket.disconnect().then(() => { if (callBack) callBack(); }); } else { if (callBack) callBack(); } } public logout() { console.log("logout request"); let self = this; let registrationId = ""; let msg: IDictionary = {}; msg["username"] = this.username; msg["registrationId"] = registrationId; if (self.socket != null) self.socket.notify("connector.entryHandler.logout", msg); this.disConnect(); delete self.socket; } public init(callback: (err, res) => void) { console.log("serverImp.init()"); let self = this; this._isConnected = false; self.socket = Pomelo; self.host = Config.Stalk.chat; self.port = parseInt(Config.Stalk.port); if (!!self.socket) { //