通过 socket.io 添加 ssl 后无法发出消息 [英] Not able emit message after adding ssl over socket.io

查看:35
本文介绍了通过 socket.io 添加 ssl 后无法发出消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Node JS 和 socket.io 构建了一个应用程序.它工作正常.由于我必须通过安全连接移动应用程序,因此我更改了代码以支持 SSL.

I had built an application using Node JS and socket.io. It was working fine. Since I had to move the application over a secure connection, I changed the code to support SSL.

但是,现在我无法执行 socket.emit.服务器和客户端都无法发出消息.

But, now I am unable to do a socket.emit. Neither server, nor the client is able to emit the messages.

连接成功,但之后什么也没有发出.

The connections are made successfully, but nothing gets emitted afterwards.

这是代码.

服务器端

var fs = require( 'fs' );

var options = {
       key:  fs.readFileSync('/etc/httpd/conf.d/ssl/*.xyz.in.key'),
       cert: fs.readFileSync('/etc/httpd/conf.d/ssl/xyz.in.crt'),
       requestCert:        true,
       rejectUnauthorized: false  // Tried both, with and without this line.
  };

var io = require( 'socket.io' ).listen( 8888, options );

io.sockets.on('connection', OnConnection );

io.set("origins", "*:*"); // Tried with and without this line.

// Called when a connection is made with a new Client.
function OnConnection ( socket )
{
    console.log( socket.id + " connected" ); // This line gets printed.
    socket.on('ev_SendMsgForApproval', OnMsgReceivedForModApproval );
}

function OnMsgReceivedForModApproval( data )
{
     console.log( data ); //This does not get executed. :(
}

客户端

var socket = io.connect("https://xyz.com:8888", {secure : true} );

// This message get called for sure.
function OnMsgSendClick()
{
    console.log( "Hi" );  // This gets printed on browser's console.
    socket.emit( 'ev_SendMsgForApproval', { chatMsg: "Hi" } );
    return false;
}

我也尝试从服务器发出,但该数据也没有到达客户端.在调试中,我能够看到服务器发出了一些东西,但没有到达客户端.

I have also tried to do emit from server, but that data, does not reach the client either. In debug I am able to see that server has emitted something, but that does not reach the client.

所有这些在没有 SSL 的情况下都可以正常工作.

All this was working fine without the SSL.

可能是什么问题?被这个困扰了很久.请帮忙.

What could be the issue? Stuck with this from a long time. Please help.

推荐答案

几天后我解决了这个问题.

I solved the problem after a few days.

问题出在客户端的以下行中.

The problem was in the following line on the client side.

var socket = io.connect("https://xyz.com:8888", {secure : true} );

我们不应该添加{secure : true},所以该行就变成了

We are not supposed to add {secure : true}, so the line simply becomes

var socket = io.connect("https://xyz.com:8888" );

我是如何知道这个解决方案的?我学会了如何分析 Web Socket使用 Chrome 开发工具的数据流量

How I came to know about this solution? I learnt how to analyse the Web Socket data traffic using Chrome Dev tools

很快我发现客户端将 {secure : true} 作为数组的第一个元素发出.这改变了整个对象的结构.由于所有这些服务器无法解析事件名称,因为第一个数组元素不包含事件名称.

Very soon I found out that the client was emitting {secure : true} as the 1st element of the array. This changed the structure of the entire object. As a result of all this server was not able to resolve the Event Name, since the first array element was not containing the event name.

我删除了 {secure : true} 并且它起作用了.:)

I removed {secure : true} and it worked. :)

这篇关于通过 socket.io 添加 ssl 后无法发出消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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