Skip to content

Instantly share code, notes, and snippets.

@dunithd
Created February 21, 2021 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dunithd/73788d0de8218755c94e5e9d21252ac3 to your computer and use it in GitHub Desktop.
Save dunithd/73788d0de8218755c94e5e9d21252ac3 to your computer and use it in GitHub Desktop.
"use strict"
var connected = false;
var socket;
function connect() {
if (! connected) {
var clientId = generateClientId(6);
socket = new WebSocket("ws://" + location.host + "/dashboard/" + clientId);
socket.onopen = function() {
connected = true;
console.log("Connected to the web socket with clientId [" + clientId + "]");
$("#connect").attr("disabled", true);
$("#connect").text("Connected");
};
socket.onmessage =function(m) {
console.log("Got message: " + m.data);
$("#totalOrders").text(m.data);
};
}
}
function generateClientId(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment