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

// npm install --save ws
const WebSocket = require('ws');

const ws = new WebSocket('ws://www.host.com/path');

ws.on('open', function open() {
  ws.send('something');
});

ws.on('message', function incoming(data) {
  console.log(data);
});
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 :: js add a tag inside span 
Javascript :: disable button js 
Javascript :: promise.race polyfill 
Javascript :: add toolbar button quill.js 
Javascript :: react native new project mac 
Javascript :: AsyncStorage.getItem undefined is not an object 
Javascript :: fat arrow function 
Javascript :: javascript how to take off a decimal 
Javascript :: remove object from array by name javascript 
Javascript :: fetching data with react 
Javascript :: discord.js set playing tag 
Javascript :: image react native 
Javascript :: cm to feet javascript 
Javascript :: date javascript format 
Javascript :: java script login system 
Javascript :: jquery preload images 
Javascript :: javascript parse date dd/mm/yyyy hh:mm:ss 
Javascript :: react focus 
Javascript :: nextjs The engine "node" is incompatible with this module. 
Javascript :: npm react dropdown 
Javascript :: select2 find option by text 
Javascript :: convert utc to pst javascript 
Javascript :: click select option to update div jquery 
Javascript :: express send code 
Javascript :: js for loops 
Javascript :: setting usestate to prop 
Javascript :: delete all the fields on the form whit jquery 
Javascript :: phone number with dashes 
Javascript :: javascript filter array multiple conditions 
Javascript :: new Date().toLocaleDateString day 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =