【问题标题】:Node js with express return connection closed before receiving a handshake response在收到握手响应之前,具有快速返回连接的节点 js 已关闭
【发布时间】:2019-10-23 01:04:16
【问题描述】:

我有一个在 nodejs 中运行的套接字,并在 html 页面中使用这个套接字,这工作正常,有时我在开发者控制台上收到错误,就像 失败:在收到握手响应之前连接已关闭。这次我的更新没有反映在用户屏幕上。实际上,每当在管理屏幕中更新更改时,我都会在 laravel 中写入登录名以将此值存储到 redis 中,并且我使用了 laravel 事件广播,并在节点 js socket.io 中读取 redis 值更改并将值推送到用户屏幕中。 我在laravel中有代码, Laravel 控制器,

public function updatecommoditygroup(Request $request)
    {
        $request_data = array();
        parse_str($request, $request_data);
        app('redis')->set("ssahaitrdcommoditydata", json_encode($request_data['commodity']));
        event(new SSAHAITRDCommodityUpdates($request_data['commodity']));
    }

在上面的这个控制器中,当 api 调用接收到时,只需将值存储到这个 redis 键中并广播事件。 在我的活动课上,

public $updatedata;

    public function __construct($updatedata)
    {
        $this->updatedata = $updatedata;
    }
    public function broadcastOn()
    {
        return ['ssahaitrdupdatecommodity'];
    }

最后我写了我的socket.io文件,如下所示,

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis({ port: 6379 } );
redis.subscribe('ssahaitrdupdatecommodity', function(err, count) {
});
io.on('connection', function(socket) {
    console.log('A client connected');
});

redis.on('pmessage', function(subscribed, channel, data) {
    data = JSON.parse(data);
    io.emit(channel + ':' + data.event, data.data);
});
redis.on('message', function(channel, message) {
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});
http.listen(3001, function(){
    console.log('Listening on Port 3001');
});

当我从管理员更新数据时,我将传递给 laravel 控制器,控制器会将接收到的数据存储到 redis 数据库中并传递给事件广播。事件广播将值传递给套接字服务器和套接字服务器推送数据每当 redis 键更改客户端页面时。 在客户端页面中,我编写了如下代码,

<script src="../assets/js/socket.io.js"></script>
var socket = io('http://ip:3001/');
socket.on("novnathupdatecommodity:App\\Events\\NOVNATHCommodityUpdates", function(data){
//received data processing in client
});

大部分时间一切正常,但有时会遇到类似问题

**VM35846 socket.io.js:7 WebSocket connection to 'ws://host:3001/socket.io/?EIO=3&transport=websocket&sid=p8EsriJGGCemaon3ASuh' failed: Connection closed before receiving a handshake response**

由于这个问题,用户页面没有得到新数据的更新。您能否请任何人帮助我解决此问题并为此问题提供最佳解决方案。

【问题讨论】:

    标签: node.js laravel redis socket.io


    【解决方案1】:

    我认为这是因为您的套接字连接超时。

    new io({
      path:,
      serveClient:,
      orgins:,
      pingTimeout:,
      pingInterval: 
    });
    

    以上是socket配置。如果您有时不配置套接字,它的行为会很奇怪。我不知道核心原因,但我也遇到过实现套接字配置解决它的类似问题。

    Socket.io Server

    应该在客户端进行类似的配置。客户端有超时选项

    Socket.io Client

    例如。

    说这是你的前端代码 您可以使用以下命令连接到套接字服务器:

    io('http://ip:3001', { path: '/demo/socket' });
    

    在你的服务器端创建连接时:

    const io = require("socket.io");
        const socket = new io({
          path: "/demo/socket",
          serveClient: false /*whether to serve the client files (true/false)*/,
          orgins: "*" /*Supports cross orgine i.e) it helps to work in different browser*/,
          pingTimeout: 6000 /*how many ms the connection needs to be opened before we receive a ping from client i.e) If the client/ front end doesnt send a ping to the server for x amount of ms the connection will be closed in the server end for that specific client*/,
          pingInterval: 6000 /* how many ms before sending a new ping packet */
        });
    socket.listen(http);
    

    注意: 为避免复杂化,请先启动 http 服务器,然后再启动套接字。 还有其他可用的选项,但以上是最常见的选项。

    我只是描述我在 github.socket_config 中的 socket.io 文档中看到的内容。希望这会有所帮助

    【讨论】:

    • 您能否在我的代码中解释一下我需要更改上述代码的地方。我无法理解路径和服务器客户端参数。这是否需要在服务器或客户端代码中更新。
    • 我已经添加了上面的代码。希望以上信息对您有所帮助
    • 是否需要在我的socket.io文件中更新这个socket配置代码。我已经更新了上面的 socket.io 完整代码。请您检查一下,让我知道在我的 socket.io 文件中的何处使用此配置。
    • 我已经更新了代码。有很多方法可以启动套接字。如果您正在配置套接字,请不要在配置之前启动它。在你的代码中 var io = require('socket.io')(http);这将立即启动套接字并侦听节点 http 服务器。您必须先配置您的套接字,然后再启动它。为了避免不必要的冲突,我只会在服务器启动后启动套接字。
    • 连接增加会不会有问题。现在单socket有超过30000并发使用这种情况会影响吗?
    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2014-05-04
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2016-06-21
    相关资源
    最近更新 更多