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

查看:148
本文介绍了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.

谢谢!

推荐答案

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

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.

更新

您还可以尝试使用'secure'选项创建连接:

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天全站免登陆