Last active
June 4, 2019 14:08
-
-
Save terence410/c0873e0e0f1be83328f7beee32ec4b0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import {SocketClient} from "./SocketClient"; | |
// const client: SocketClient = new SocketClient("http://127.0.0.1", "userId=1"); | |
import Debug from "debug"; | |
import {EventEmitter} from "events"; | |
import * as SocketIOClient from "socket.io-client"; | |
const debug = Debug("app:SocketClient"); | |
export class SocketClient extends EventEmitter { | |
private _client: SocketIOClient.Socket; | |
private _socketOptions = { | |
transports: ["websocket"], | |
}; | |
constructor(host: string, param: string = "") { | |
super(); | |
const uri: string = `${host}${param ? ("?" + param) : ""}`; | |
debug("connect to socket: ", uri); | |
this._client = SocketIOClient.default(uri, this._socketOptions); | |
this._client.on("connect", this._onConnect); | |
this._client.on("message", this._onMessage); | |
this._client.on("disconnect", this._onDisconnect); | |
this._client.on("error", this._onError); | |
this._client.on("pong", this._onPong); | |
} | |
private _onConnect = () => { | |
debug("connected"); | |
} | |
private _onMessage = (data: any) => { | |
debug("received message", data); | |
} | |
private _onDisconnect = () => { | |
debug("disconnected"); | |
} | |
private _onError = (errorMessage: string) => { | |
debug("error: " + errorMessage); | |
} | |
private _onPong = () => { | |
debug("pong"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment