简体   繁体   English

无法连接安全的Websocket

[英]Unable to connect secured websocket

Using nodejs this client code isn't working: 使用nodejs时,此客户端代码不起作用:

var sock= require('websocket').w3cwebsocket;


var sock= new W3CWebSocket("wss://" + 'www.mysite.com'+ ':31333');

sock.onopen = function (evt)
    {
        console.log("it never reaches here");


//..................
}

Actually it was working when my nodejs server wasn't using SSL so this code used to work: 实际上,当我的nodejs服务器未使用SSL时,它就可以正常工作,因此该代码可以正常工作:

var sock= new W3CWebSocket("ws://" + 'www.mysite.com'+ ':31333');

The server code is looks like this: 服务器代码如下所示:

var ws = require("./nodejs-websocket");
var fs = require('fs');

var options = {
    secure: true,
    key: fs.readFileSync('ssl/mysite.com.key'),
    cert: fs.readFileSync('ssl/mysite.com.crt')
};

var server = ws.createServer(options, function (conn)
    {
        console.log("New connection")
        conn.on("text", function (str)
            {

               //some code here

            });
        conn.on("close", function (code, reason)
            {
                console.log("Connection closed")
            });

    }).listen(31333);

From browser I'm able to communicate to this server using Websockets. 通过浏览器,我可以使用Websockets与该服务器通信。 But not from the Linux commandline. 但不是从Linux命令行。 How to make it working from Linux commandline? 如何使其从Linux命令行运行?

I can not speak much to the library you are using. 我不能对您正在使用的图书馆说太多话。 But I had to have secure WebSockets as well for a recent . 但是最近我也必须有安全的WebSockets。 and used this library instead socket.io-redis socket.io They specifically allow you to use secure connections. 并改用了这个库socket.io-redis socket.io它们专门允许您使用安全连接。 Below is a code example of how this works and the above links will lead to the documentation. 下面是一个代码示例,说明了如何工作,上面的链接将指向文档。

var redis = require('redis');

var client = redis.createClient(config.redisPort,config.redis,
  {auth_pass: config.reDISCACHEKEY, tls: {servername: config.redis}}); //creates a new client
var sclient=redis.createClient(config.redisPort,config.redis,
  {auth_pass: config.reDISCACHEKEY, tls: {servername: config.redis}}); //creates a new client

var io = require('socket.io').listen(app);
var redis = require('socket.io-redis');

io.adapter(redis({pubClient:client,subClient:sclient}))

io.on('connection', function(socket){

  client.set(socket.handshake.query.userName, socket.id);

    //sets what happens when a socket disconnects
    //deletes socket information

   socket.on('disconnect', function(){
     let name=socket.handshake.query.userName;
     client.del(name, function(err, reply) {
     });
   });
   socket.on('message', function(msg, func){
     let name=socket.handshake.query.userName;
     let sockectObj=msg;
     io.emit('Message', sockectObj);
   });
 });

Let me know if more is needed 让我知道是否需要更多

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

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