简体   繁体   中英

Socket.IO + Node.js + SSL

I would like to run simply chat using Node.js (0.5.11) and Socket.IO. I've working it with pure HTTP but i need to start SSL. I've generate key and certificate like in the wiki: http://nodejs.org/api/tls.html#tls_tls_ssl

The code i use:

app.js:

var tls = require('tls');
var fs = require('fs');

var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem'),
};

var server = tls.createServer(options, function(cleartextStream) {
  console.log('server connected',
              cleartextStream.authorized ? 'authorized' : 'unauthorized');
  cleartextStream.write("welcome!\n");
  cleartextStream.setEncoding('utf8');
  cleartextStream.pipe(cleartextStream);
});
server.listen(8000, function() {
  console.log('server bound');
});

The server starts good. Problem begins when i want to refer to socket.io on index.php:

<script src="https://localhost:8000/socket.io/socket.io.js"></script>

Firebug gives me status Aborted and i cannot work with "io" object becouse it is not loaded. I've also try "http://local..." in the src attribute but with no change. What am i doing wrong? Could someone give me an advice or good tutorial how to run Socket.IO wth SSL?

Thanks already for reply

<script src='/socket.io/socket.io.js'></script>

You just need this as it will autodiscover, just in case you forget to update port and address if connecting to another address.

Where is socketio included in your server? that seems to be the problem

var sockio = require("socket.io");
var io = sockio.listen(server);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM