node.jsでwssプロトコルを使う

wss://~で通信したい

ssl証明書の作成
http://www.maruko2.com/mw/Apache/SSL%E8%87%AA%E5%B7%B1%E8%A8%BC%E6%98%8E%E6%9B%B8%E3%81%AE%E4%BD%9C%E6%88%90%E3%81%A8mod_ssl%E3%81%AE%E8%A8%AD%E5%AE%9A

openssl genrsa -aes128 1024 > server.key
openssl req -new -key server.key > server.csr
openssl x509 -in server.csr -days 365 -req -signkey server.key > server.crt
mv server.key server.key.back
openssl rsa -in server.key.back > server.key

wssを使うサーバプログラム
https://groups.google.com/forum/#!topic/nodejs_jp/JeL_HL14Q6Q
https://gist.github.com/shigeki/d209c7269611964f4bfb
server.js

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('./server.crt'),
  key: fs.readFileSync('./server.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);
});

クライアント
https://github.com/request/request/issues/418
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" を指定する
client.js

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" 
var WebSocket = require('ws')
  , ws = new WebSocket('wss://localhost:8443', ['binary', 'base64']);
ws.on('open', function() {
    ws.send('hello from client');
});
ws.on('message', function(message) {
    console.log('received: %s', message);
});