Answers for "javascript websocket server example"

1

websocket sample code js

/*
 * Link starts with ws:// or wss://
 * ws:// for non ssl
 * wss:// for ssl
*/
let ws = new WebSocket('LINK_HERE')
        
ws.addEventListener('open', function (event) {
  //WebSocket Connected
  console.log('connected')
})

ws.addEventListener('message', function (event) {
  //Received message
  console.log('MESSAGE RECEIVED')
  console.log(event.data) //MESSAGE DATA
})

ws.addEventListener('close', function (event) {
  //WebSocket disconnected
  console.log('disconnected')
})

// To send data
ws.send('hello')

// Only text formats allowed to send, if data is in json format use
ws.send(JSON.stringify(object))
Posted by: Guest on June-03-2021
3

js connect to websocket

var exampleSocket = new WebSocket("wss://www.example.com/socketserver", "protocolOne");
Posted by: Guest on February-22-2020

Code answers related to "javascript websocket server example"

Code answers related to "Javascript"

Browse Popular Code Answers by Language