简体   繁体   English

Websocket在刷新时无法连接

[英]Websocket does not connect on refresh

I have got a Websocket server using Ratchet/PHP: 我有一个使用Ratchet / PHP的Websocket服务器:

<?php
require __DIR__.'/../vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Mediator;

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Mediator()
        )
    ),
    9000
);

$server->run();
?>

Mediator class: 调解员班:

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Mediator implements MessageComponentInterface {
    protected $clients = [];

    public function onOpen(ConnectionInterface $conn) {
        $this->clients[$conn->resourceId] = $conn;
        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        echo "Incoming: $msg\n";
    }

    public function onClose(ConnectionInterface $conn) {
        unset($this->clients[$conn->resourceId]);
        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}
?>

Now on the client side, I have this basic JS code: 现在在客户端,我有这个基本的JS代码:

let ws = new WebSocket('wss://localhost:8443');
ws.addEventListener('open', () => {
    ws.send('Hello!');
});
ws.addEventListener('message', event => {
    alert(event.data);
});

It does work (I can send and receive messages), however here's the problem: 它确实有效(我可以发送和接收消息),但问题是:

When visiting the page for the first time, a connection with the websocket server is established and works fine. 首次访问该页面时,建立与websocket服务器的连接并正常工作。 When I close the page, the connection is closed (as it should). 当我关闭页面时,连接将关闭(应该如此)。 However, when I refresh the page the connection is closed (on unloading the page, this is normal) but when the page is loaded again, no connection is made to the websocket server. 但是,当我刷新页面时,连接被关闭(在卸载页面时,这是正常的)但是当再次加载页面时,没有与websocket服务器建立连接。 I have to refresh again to make the script connect. 我必须再次刷新以使脚本连接。 This should not happen, right? 这不应该发生,对吧? I have no idea why this is happening, what's causing this. 我不知道为什么会发生这种情况,导致这种情况的原因。

maybe add an event listener to close your ws javascript WebSocket before trying to reopen it. 也许添加一个事件监听器来关闭你的ws javascript WebSocket然后再尝试重新打开它。

let ws = new WebSocket('wss://localhost:8443');
ws.addEventListener('open', () => {
    ws.send('Hello!');
});


$(window).unload(function () {
   ws.close();
});

also include possible console errors, as this looks like a issue with your javascript WebSocket logic, but I have been reading more info about Rachet/PHP and I would appreciate additional information on your backend logic. 还包括可能的控制台错误,因为这看起来像你的javascript WebSocket逻辑的问题,但我一直在阅读有关Rachet / PHP的更多信息 ,我将非常感谢有关你的后端逻辑的更多信息。

Update 更新

I suggest you to check out their demo page and debug both your code and your network requests. 我建议你查看他们的演示页面并调试你的代码和你的网络请求。 Start re-creating a 1-1 exactly identical working functionality, then implement your changes and understand what causes your errors: 开始重新创建1-1完全相同的工作功能,然后实施更改并了解导致错误的原因:

1) Review your network request and persist the log, read them and report any strange issues 1)检查您的网络请求并保留日志,阅读并报告任何奇怪的问题

在此输入图像描述

2) Review their code for the demo 2)查看他们的演示代码

在此输入图像描述

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

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