dongxun7962 2019-04-26 14:58
浏览 486

Websocket在刷新时无法连接

I have got a Websocket server using Ratchet/PHP:

<?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})
";
    }

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

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

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

Now on the client side, I have this basic JS code:

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. 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. 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.

  • 写回答

1条回答 默认 最新

  • doujin4031 2019-05-07 06:48
    关注

    maybe add an event listener to close your ws javascript WebSocket before trying to reopen it.

    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.

    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) Review your network request and persist the log, read them and report any strange issues

    enter image description here

    2) Review their code for the demo

    enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入