Skip to content

Instantly share code, notes, and snippets.

@chr15m
Created July 6, 2018 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chr15m/f5fcb734ef3e6e57e62e46e3ce64596a to your computer and use it in GitHub Desktop.
Save chr15m/f5fcb734ef3e6e57e62e46e3ce64596a to your computer and use it in GitHub Desktop.
Minimal Javascript websocket client/server
// server
ws = require('ws');
wss = new ws.Server({ port: 8033 });
wss.on('connection', function(s) {
console.log("got connection", s);
s.on('message', function incoming(message) {
console.log("received", message);
});
});
// client
var ws = new WebSocket("ws://localhost:8033/");
ws.onopen = function() {
console.log("open", arguments);
ws.send("Hello Mr. Server!\r\n");
};
ws.onmessage = function() { console.log("message", arguments); };
ws.onclose = function() { console.log("close"); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment