简体   繁体   English

Socket.IO + Node.js + SSL

[英]Socket.IO + Node.js + SSL

I would like to run simply chat using Node.js (0.5.11) and Socket.IO. 我想使用Node.js(0.5.11)和Socket.IO运行简单的聊天。 I've working it with pure HTTP but i need to start SSL. 我正在使用纯HTTP进行操作,但是我需要启动SSL。 I've generate key and certificate like in the wiki: http://nodejs.org/api/tls.html#tls_tls_ssl 我已经在Wiki中生成了密钥和证书: http : //nodejs.org/api/tls.html#tls_tls_ssl

The code i use: 我使用的代码:

app.js: 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: 当我想在index.php上引用socket.io时,问题就开始了:

<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. Firebug使我的状态已中止,并且由于未加载“ io”对象而无法使用。 I've also try "http://local..." in the src attribute but with no change. 我也尝试了src属性中的“ http:// local ...”,但没有任何变化。 What am i doing wrong? 我究竟做错了什么? Could someone give me an advice or good tutorial how to run Socket.IO wth SSL? 有人可以给我一个建议还是一个很好的教程,介绍如何通过SSL运行Socket.IO?

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? 服务器中的哪里包含socketio? that seems to be the problem 这似乎是问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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