使用node.js中的socket.io设置服务器-服务器SSL通信 [英] Setup Server-Server SSL communication using socket.io in node.js

查看:112
本文介绍了使用node.js中的socket.io设置服务器-服务器SSL通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过SSL连接使用socket.io设置服务器到服务器的链接.这是我的赞美诗:

I'm trying to setup a server to server link using socket.io over ssl connection. This is my exmaple:

/**
 * Server
 */


var app = require('express')();
var config = require('./config');
var https = require('https');
var http = require('http');
var fs = require('fs');
var server = https.createServer({key: fs.readFileSync(config.ssl.key), cert: fs.readFileSync(config.ssl.cert), passphrase: config.ssl.passphrase}, app);
//var server = http.createServer(app);
var io = require('socket.io').listen(server);

server.listen(config.port);

app.get('/', function (req, res) {
    res.send('Server');
  //res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});



/**
 * Client
 */


var io = require('socket.io-client');
//var socket = io.connect('http://localhost', {port: 8088});
var socket = io.connect('https://localhost', {secure: true, port: 8088});
  socket.on('connect', function(){
    socket.on('event', function(data){});
    socket.on('disconnect', function(){});
  });

在没有SSL的情况下运行时,代码工作正常.我怀疑这可能是我的自签名证书未被接受,但是我不知道如何使客户接受它.

The code works fine when ran without SSL. I suspect it could be my self signed certificate not being accepted, but I do not know how to make the client accept it.

请教我如何: 1.接受自签名SSL证书. 或者 2.帮我以其他方式进行这项工作.

Please show me how to: 1. Accept self-signed SSL certificate. or 2. Help me make this work some other way.

提前谢谢.

推荐答案

再进行一些搜索后,将其添加到客户端即可使其工作:

After some more searching, adding this in the client makes it work:

require('https').globalAgent.options.rejectUnauthorized = false;

require('https').globalAgent.options.rejectUnauthorized = false;

/**
 * Client
 */


var io = require('socket.io-client');
//var socket = io.connect('http://localhost', {port: 8088});

require('https').globalAgent.options.rejectUnauthorized = false; 

var socket = io.connect('https://localhost', {secure: true, port: 8088});
  socket.on('connect', function(){
    socket.on('event', function(data){});
    socket.on('disconnect', function(){});
  });

这篇关于使用node.js中的socket.io设置服务器-服务器SSL通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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