js socket connection code example
Example 1: javascript websocket example code
var Socket = new WebSocket('ws://' + window.location.hostname + ':81/');
Socket.send("pass your data here, and it'll be String");
Example 2: js connect to websocket
var exampleSocket = new WebSocket("wss://www.example.com/socketserver", "protocolOne");
Example 3: js socket.emit
const socket = io('ws://localhost:3000');
socket.on('connect', () => {
socket.send('Hello!');
socket.emit('salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from([1, 2, 3, 4]));});
socket.on('message', data => {
console.log(data);
});
socket.on('greetings', (elem1, elem2, elem3) => {
console.log(elem1, elem2, elem3);
});