在收到握手响应之前,具有快速返回连接的Node js已关闭

时间:2019-06-07 13:57:19

标签: node.js laravel redis socket.io

我有一个在Node.js中运行的套接字,并且在html页面中使用此套接字,这工作正常,有时我在开发者控制台上收到错误,例如 失败:在收到握手响应之前,连接已关闭。目前,我的更新没有反映在用户屏幕上。实际上,无论何时在管理屏幕中更新更改,我都会在laravel中写入登录名以将该值存储到redis中,并且我使用了laravel事件广播,并在node 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');
});

当我从admin更新数据时,我将其传递给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**

此问题的用户页面无法获取新数据的更新。您能否请任何人帮助我解决此问题,并为该问题提供最佳解决方案。

1 个答案:

答案 0 :(得分:0)

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

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

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

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文档中看到的内容。希望这会有所帮助