Skip to content

Instantly share code, notes, and snippets.

@robzhu
Created January 4, 2019 23:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robzhu/5958d5f9af1a0d1650a04fe29a29fa1f to your computer and use it in GitHub Desktop.
Save robzhu/5958d5f9af1a0d1650a04fe29a29fa1f to your computer and use it in GitHub Desktop.
WebSocket client.js
const WebSocket = require('ws');
const readline = require('readline');
const url = process.argv[2];
const ws = new WebSocket(url);
ws.on('open', () => console.log('connected'));
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
console.log('disconnected');
process.exit();
});
readline.createInterface({
input: process.stdin,
output: process.stdout,
}).on('line', data => {
ws.send(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment