【问题标题】:Socket.IO client cannot send handshake parameterSocket.IO 客户端无法发送握手参数
【发布时间】:2017-10-04 20:20:33
【问题描述】:

这是我的代码:

var socket = require('socket.io-client')('http://127.0.0.1:3000/printers', {
    query: "shop=" + "123456",
    transports: ["websocket"]
});

如果我删除查询,我可以连接到套接字。我哪里错了?

【问题讨论】:

    标签: socket.io


    【解决方案1】:

    您的客户端代码似乎没有任何问题。我可以通过复制和粘贴您的代码来连接。

    我怀疑问题出在您的服务器端代码中。这是我与您的客户端代码一起使用的示例:

    var http = require('http');
    var io = require('socket.io');
    
    var server = http.createServer(function(req,res){
      res.writeHead(200);
      res.end('Hello, World!\n');
    });
    
    server.listen(80);
    
    var socket = io.listen(server);
    
    socket.use(function(socket, next){
        console.log("Query: ", socket.handshake.query);
    
        if (socket.handshake.query.shop == '123456') {
            next();
        }
    
        next(new Error('You cannot use that shop'));
    });
    
    socket.on('connection', function(client) { 
        console.log('New Connection');
    });
    

    我能够在“socket.use”函数中获取查询数据。如果我不调用 next() 函数,客户端永远不会得到服务器收到响应并连接的消息。

    我建议查看thread 中使用的示例。

    【讨论】:

    • 如果这不能解决您的问题,您能否发布更多代码。 ?
    • 我发现问题出在最新版本的 Socket.IO Client 上。我降级到 1.4.7 版本,它工作。
    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2018-10-19
    • 2012-11-11
    相关资源
    最近更新 更多