1/*
2 * Link starts with ws:// or wss://
3 * ws:// for non ssl
4 * wss:// for ssl
5*/
6let ws = new WebSocket('LINK_HERE')
7
8ws.addEventListener('open', function (event) {
9 //WebSocket Connected
10 console.log('connected')
11})
12
13ws.addEventListener('message', function (event) {
14 //Received message
15 console.log('MESSAGE RECEIVED')
16 console.log(event.data) //MESSAGE DATA
17})
18
19ws.addEventListener('close', function (event) {
20 //WebSocket disconnected
21 console.log('disconnected')
22})
23
24// To send data
25ws.send('hello')
26
27// Only text formats allowed to send, if data is in json format use
28ws.send(JSON.stringify(object))
1var Socket = new WebSocket('ws://' + window.location.hostname + ':81/'); // The '81' here is the Port where the WebSocket server will communicate with
2// The instance of the WebSocket() class (i.e. Socket here), must need to be globally defined
3
4Socket.send("pass your data here, and it'll be String"); // This method one can call locally