Skip to content

Instantly share code, notes, and snippets.

@robzhu
Last active September 22, 2020 06:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robzhu/eb5b5df70b33febf2add978b04e5da19 to your computer and use it in GitHub Desktop.
Save robzhu/eb5b5df70b33febf2add978b04e5da19 to your computer and use it in GitHub Desktop.
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 => {
const message = JSON.stringify({action: 'echo', data: input});
ws.send(message);
});
@davew723
Copy link

davew723 commented Jan 4, 2020

Shouldn't this line...
const message = JSON.stringify({action: 'echo', data: input});
... be ...
const message = JSON.stringify({action: 'echo', data: data});
?

@philmerrell
Copy link

line 18 & 19:

const message = JSON.stringify({action: 'echo', data });
ws.send(message);

@robzhu
Copy link
Author

robzhu commented Apr 6, 2020

Thanks @philmerrell, fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment