Skip to content

Instantly share code, notes, and snippets.

@sean3z
Created March 18, 2018 02:42
Show Gist options
  • Save sean3z/3a645922a822342a33a9c9e8b2a0a40b to your computer and use it in GitHub Desktop.
Save sean3z/3a645922a822342a33a9c9e8b2a0a40b to your computer and use it in GitHub Desktop.
Websocket example (without Socket.io)
var server = require('websocket').server, http = require('http');
var socket = new server({
httpServer: http.createServer().listen(1337)
});
socket.on('request', function(request) {
var connection = request.accept(null, request.origin);
connection.on('message', function(message) {
console.log(message.utf8Data);
connection.sendUTF('hello');
setTimeout(function() {
connection.sendUTF('this is a websocket example');
}, 1000);
});
connection.on('close', function(connection) {
console.log('connection closed');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment