Skip to content

Instantly share code, notes, and snippets.

@shigeki
Created April 23, 2014 11:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shigeki/d209c7269611964f4bfb to your computer and use it in GitHub Desktop.
Save shigeki/d209c7269611964f4bfb to your computer and use it in GitHub Desktop.
sample code of wss server by websocket.io
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