Client: rename internal WebSocket event handlers
This commit is contained in:
parent
98c89f0702
commit
e746a4e8ca
|
|
@ -4,55 +4,76 @@ class Client {
|
||||||
constructor(wsUri) {
|
constructor(wsUri) {
|
||||||
this.wsUri = wsUri;
|
this.wsUri = wsUri;
|
||||||
|
|
||||||
// Create WebSocket and set callbacks
|
// Create WebSocket and set internal callbacks
|
||||||
console.log("Initialize Client...")
|
console.log("Initialize Client...")
|
||||||
this.webSocket = new WebSocket(wsUri);
|
this.webSocket = new WebSocket(wsUri);
|
||||||
this.webSocket.onopen = this.onOpen.bind(this);
|
this.webSocket.onopen = this._onSocketOpen.bind(this);
|
||||||
this.webSocket.onclose = this.onClose.bind(this);
|
this.webSocket.onclose = this._onSocketClose.bind(this);
|
||||||
this.webSocket.onmessage = this.onMessage.bind(this);
|
this.webSocket.onerror = this._onSocketError.bind(this);
|
||||||
this.webSocket.onerror = this.onError.bind(this);
|
this.webSocket.onmessage = this._onSocketMessage.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebSocket event handlers
|
// Internal WebSocket event handlers
|
||||||
onOpen(evt) {
|
_onSocketOpen(evt) {
|
||||||
console.log("Connected to " + this.wsUri);
|
console.log("Connected to " + this.wsUri);
|
||||||
|
|
||||||
// Send init command containing chat ID and nickname
|
// Send init command containing chat ID and nickname
|
||||||
this.sendInit();
|
this.sendInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
onClose(evt) {
|
_onSocketClose(evt) {
|
||||||
console.log("Connection closed (code " + evt.code + ").");
|
console.log("Connection closed (code " + evt.code + ").");
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(evt) {
|
_onSocketError(evt) {
|
||||||
console.log("Received: " + evt.data);
|
|
||||||
this.parseMessage(evt.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
onError(evt) {
|
|
||||||
console.error("Connection error: ", evt);
|
console.error("Connection error: ", evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command senders
|
_onSocketMessage(evt) {
|
||||||
sendInit() {
|
console.log("Received: " + evt.data);
|
||||||
// Define command as JSON object
|
this._parseMessage(evt.data);
|
||||||
let initObj = {
|
|
||||||
action: "init",
|
|
||||||
chat_id: "42",
|
|
||||||
nickname: "binaryDiv"
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send command as JSON string
|
|
||||||
let initJson = JSON.stringify(initObj);
|
|
||||||
console.log("Sending init: " + initJson);
|
|
||||||
this.webSocket.send(initJson);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message parsing
|
/**
|
||||||
parseMessage(msgString) {
|
* Sends an arbitrary command as JSON.
|
||||||
|
*
|
||||||
|
* commandObj: The command as an object ('action' specifies type of command).
|
||||||
|
*/
|
||||||
|
sendCommand(commandObj) {
|
||||||
|
const commandJson = JSON.stringify(commandObj);
|
||||||
|
console.log("Sending command: " + commandJson);
|
||||||
|
this.webSocket.send(commandJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the 'init' command which sets up the session. Also sets the chat ID and nickname.
|
||||||
|
*/
|
||||||
|
sendInit() {
|
||||||
|
this.sendCommand({
|
||||||
|
action: "init",
|
||||||
|
chat_id: "42",
|
||||||
|
nickname: "binaryDiv",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the 'message' command which sends a chat message to the chat.
|
||||||
|
*
|
||||||
|
* msgText: The text of the chat message.
|
||||||
|
*/
|
||||||
|
sendChatMessage(msgText) {
|
||||||
|
this.sendCommand({
|
||||||
|
action: "message",
|
||||||
|
text: msgText,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses an incoming JSON message and dispatches specific events.
|
||||||
|
*/
|
||||||
|
_parseMessage(msgString) {
|
||||||
try {
|
try {
|
||||||
let msg = JSON.parse(msgString);
|
const msg = JSON.parse(msgString);
|
||||||
|
|
||||||
switch (msg.type) {
|
switch (msg.type) {
|
||||||
// Response to "init" command
|
// Response to "init" command
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue