Created
March 18, 2018 02:42
-
-
Save sean3z/3a645922a822342a33a9c9e8b2a0a40b to your computer and use it in GitHub Desktop.
Websocket example (without Socket.io)
This file contains 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
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