Created
February 21, 2021 07:48
-
-
Save dunithd/73788d0de8218755c94e5e9d21252ac3 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
"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