Client: encapsulate WebSocket client in Client class
This commit is contained in:
parent
fd6a40cb0d
commit
3993ae9c44
|
|
@ -1,47 +1,43 @@
|
||||||
// Constants and variables
|
// Constants and variables
|
||||||
// const wsUri = "ws://localhost:32715";
|
// const wsUri = "ws://localhost:32715";
|
||||||
const wsUri = "wss://chat.glitch-in.space:443/ws/";
|
const wsUri = "wss://chat.glitch-in.space:443/ws/";
|
||||||
let websocket;
|
|
||||||
|
|
||||||
|
class Client {
|
||||||
|
constructor(wsUri) {
|
||||||
|
this.wsUri = wsUri;
|
||||||
|
|
||||||
// Initialization
|
// Create WebSocket and set callbacks
|
||||||
function init() {
|
console.log("Initialize Client...")
|
||||||
console.log("Init...");
|
this.webSocket = new WebSocket(wsUri);
|
||||||
openWebSocket();
|
this.webSocket.onopen = this.onOpen.bind(this);
|
||||||
}
|
this.webSocket.onclose = this.onClose.bind(this);
|
||||||
|
this.webSocket.onmessage = this.onMessage.bind(this);
|
||||||
// Open WebSocket
|
this.webSocket.onerror = this.onError.bind(this);
|
||||||
function openWebSocket() {
|
|
||||||
websocket = new WebSocket(wsUri);
|
|
||||||
websocket.onopen = function(evt) { onOpen(evt) };
|
|
||||||
websocket.onclose = function(evt) { onClose(evt) };
|
|
||||||
websocket.onmessage = function(evt) { onMessage(evt) };
|
|
||||||
websocket.onerror = function(evt) { onError(evt) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebSocket event handlers
|
// WebSocket event handlers
|
||||||
function onOpen(evt) {
|
onOpen(evt) {
|
||||||
console.log("Connected to " + wsUri + ".");
|
console.log("Connected to " + wsUri);
|
||||||
|
|
||||||
// Send init command containing chat ID and nickname
|
// Send init command containing chat ID and nickname
|
||||||
sendInit();
|
this.sendInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
function onClose(evt) {
|
onClose(evt) {
|
||||||
console.log("Connection closed (code " + evt.code + ").");
|
console.log("Connection closed (code " + evt.code + ").");
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(evt) {
|
onMessage(evt) {
|
||||||
console.log("Received: " + evt.data);
|
console.log("Received: " + evt.data);
|
||||||
parseMessage(evt.data);
|
this.parseMessage(evt.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError(evt) {
|
onError(evt) {
|
||||||
console.error("Connection error: ", evt);
|
console.error("Connection error: ", evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command senders
|
// Command senders
|
||||||
function sendInit() {
|
sendInit() {
|
||||||
// Define command as JSON object
|
// Define command as JSON object
|
||||||
let initObj = {
|
let initObj = {
|
||||||
action: "init",
|
action: "init",
|
||||||
|
|
@ -52,11 +48,11 @@ function sendInit() {
|
||||||
// Send command as JSON string
|
// Send command as JSON string
|
||||||
let initJson = JSON.stringify(initObj);
|
let initJson = JSON.stringify(initObj);
|
||||||
console.log("Sending init: " + initJson);
|
console.log("Sending init: " + initJson);
|
||||||
websocket.send(initJson);
|
this.webSocket.send(initJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message parsing
|
// Message parsing
|
||||||
function parseMessage(msgString) {
|
parseMessage(msgString) {
|
||||||
try {
|
try {
|
||||||
let msg = JSON.parse(msgString);
|
let msg = JSON.parse(msgString);
|
||||||
|
|
||||||
|
|
@ -83,8 +79,9 @@ function parseMessage(msgString) {
|
||||||
console.error("Error parsing message JSON: " + e.message);
|
console.error("Error parsing message JSON: " + e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Run script after page is loaded
|
// Run script after page is loaded
|
||||||
$(function() {
|
$(function() {
|
||||||
init();
|
let client = new Client(wsUri);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue