9951 explained code solutions for 126 technologies


nodejsWebsocket client example


let ws = require('websocket');
let wsc = new ws.client;

wsc.on('connect', function(connection) {
  connection.sendUTF('Hello');

  connection.on('message', function(message) {
    console.log("Received: " + message.utf8Data);
    // connection.close();
  });
});

wsc.connect('ws://echoof.me:8111/');ctrl + c
require('websocket')

import websocket lib to create websocket client

new ws.client

create new websocket client object

wsc.on('connect'

specify what to do when client gets connected to websocket server

connection.sendUTF

send message to server

connection.on('message'

specify what to do when client received message from server

connection.close()

close connection (and exit)

wsc.connect

connect to websocket server

echoof.me:8111

public echo websocket server


Usage example

let ws = require('websocket');
let wsc = new ws.client;

wsc.on('connect', function(connection) {
  connection.sendUTF('Hello');

  connection.on('message', function(message) {
    console.log("Received: " + message.utf8Data);
    connection.close();
  });
});

wsc.connect('ws://echoof.me:8111/');
output
Received: I got "Hello" from you (1.2.3.4)