Created
July 6, 2018 02:02
-
-
Save chr15m/f5fcb734ef3e6e57e62e46e3ce64596a to your computer and use it in GitHub Desktop.
Minimal Javascript websocket client/server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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