Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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))
Comment

javascript websocket example code

var Socket = new WebSocket('ws://' + window.location.hostname + ':81/'); // The '81' here is the Port where the WebSocket server will communicate with
// The instance of the WebSocket() class (i.e. Socket here), must need to be globally defined

Socket.send("pass your data here, and it'll be String"); // This method one can call locally
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery serialize 
Javascript :: nx serve 2 applications 
Javascript :: loop through object and get a certain values 
Javascript :: how to add keyboard shortcuts javascript 
Javascript :: puppeteer clear input 
Javascript :: active menu adminlte 3 using jquery 
Javascript :: convert json to base64 javascript 
Javascript :: loop json 
Javascript :: get keys of dictionary js 
Javascript :: js time difference in minutes 
Javascript :: how to copy text on clipboard in react 
Javascript :: chrome max local storage size 
Javascript :: jquery scroll to element 
Javascript :: javascript loop through string 
Javascript :: javascript one time event listener 
Javascript :: Jquery Scroll on div using anchor tag is not Working properly 
Javascript :: fibonacci js code 
Javascript :: moment date add 
Javascript :: react or vue 
Javascript :: disable copy past jquery 
Javascript :: ecmascript 
Javascript :: js deep copy array 
Javascript :: js query string 
Javascript :: loop n times js 
Javascript :: angular ng build Maximum call stack size exceeded 
Javascript :: javascript remove item from array in loop 
Javascript :: validate json file programmatically in python 
Javascript :: nativescript vue back button handler 
Javascript :: stop next script when ajaxcall 
Javascript :: ionic (Emitted value instead of an instance of Error 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =