-
-
Save shigeki/d209c7269611964f4bfb to your computer and use it in GitHub Desktop.
sample code of wss server by websocket.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 ws = require('websocket.io'); | |
var https = require('https'); | |
var fs = require('fs'); | |
var hostname = 'example.jp'; | |
var port = 8443; | |
var url = 'wss://' + hostname + ':' + port + '/'; | |
var opts = { | |
cert: fs.readFileSync('./ssl.cert'), | |
key: fs.readFileSync('./ssl.key') | |
}; | |
var content = '<!doctype html><html><head><title>WSS Test</title></head><body><div id="out"></div><script>' + | |
'var wss = new WebSocket("'+ url + '");' + | |
'var out = document.getElementById("out");'+ | |
'wss.onmessage = function(e) {out.innerHTML = "<div>" + e.data + "</div>";}'+ | |
'</script></body></html>'; | |
var ssl_server = https.createServer(opts, function(req, res) { | |
res.writeHead(200, {'Content-Type': 'text/html', | |
'Content-Length': content.length} | |
); | |
res.end(content); | |
}); | |
var wss = ws.attach(ssl_server); | |
wss.on('connection', function(socket) { | |
var counter = 0; | |
setInterval(function() { | |
socket.send('Hello wss from Server: ' + counter++); | |
}, 500); | |
}); | |
ssl_server.listen(port, function() { | |
console.log('Listening on ' + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment