node.js、带 SSL 的 socket.io [英] node.js, socket.io with SSL

查看:64
本文介绍了node.js、带 SSL 的 socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的 SSL 证书运行 socket.io,但是它无法连接.

I'm trying to get socket.io running with my SSL certificate however, it will not connect.

我的代码基于聊天示例:

I based my code off the chat example:

var https = require('https');
var fs = require('fs');
/**
 * Bootstrap app.
 */
var sys = require('sys')
require.paths.unshift(__dirname + '/../../lib/');

/**
* Module dependencies.
*/

var express = require('express')
  , stylus = require('stylus')
  , nib = require('nib')
  , sio = require('socket.io');

/**
 * App.
 */
var privateKey = fs.readFileSync('../key').toString();
var certificate = fs.readFileSync('../crt').toString();
var ca = fs.readFileSync('../intermediate.crt').toString();

var app = express.createServer({key:privateKey,cert:certificate,ca:ca });


/**
 * App configuration.
 */

...

/**
 * App routes.
 */

app.get('/', function (req, res) {
  res.render('index', { layout: false });
});

/**
 * App listen.
 */

app.listen(443, function () {
  var addr = app.address();
  console.log('   app listening on http://' + addr.address + ':' + addr.port);
});

/**
 * Socket.IO server (single process only)
 */

var io = sio.listen(app,{key:privateKey,cert:certificate,ca:ca});
...

如果我删除 SSL 代码它运行良好,但是我收到一个请求 http://domain.com/socket.io/1/?t=1309967919512

If I remove the SSL code it runs fine, however with it I get a request to http://domain.com/socket.io/1/?t=1309967919512

注意它不是在尝试 https,这会导致它失败.

Note it's not trying https, which causes it to fail.

我正在 chrome 上进行测试,因为它是此应用程序的目标浏览器.

I'm testing on chrome, since it is the target browser for this application.

如果这是一个简单的问题,我很抱歉,我是 node/socket.io 新手.

I apologize if this is a simple question, I'm a node/socket.io newbie.

谢谢!

推荐答案

使用安全 URL 进行初始连接,即使用https://"代替http://".如果选择了 WebSocket 传输,那么 Socket.IO 也应该自动为 WebSocket 连接使用wss://"(SSL).

Use a secure URL for your initial connection, i.e. instead of "http://" use "https://". If the WebSocket transport is chosen, then Socket.IO should automatically use "wss://" (SSL) for the WebSocket connection too.

更新:

您也可以尝试使用安全"选项创建连接:

You can also try creating the connection using the 'secure' option:

var socket = io.connect('https://localhost', {secure: true});

这篇关于node.js、带 SSL 的 socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆