dr5648 2018-10-31 06:31
浏览 245

在客户端断开连接之前,Websocket不会发送消息

I'm learning WebSocket using PHP library Ratchet. But I have a problem sending the message from client/browser. I have tried using chrome and firefox.

The message is not sent to the server until I disconnect the client by closing the tab or refresh the browser.

Update: My server use Centos 7 with firewalld enabled.

After I close the browser tab, the server output is like this:

Connection 73 sending message "tes" to 1 other connection
Connection 73 has disconnected

Here is the javascript code:

conn = new WebSocket('ws://websocket.develop.local:8080');
            conn.onopen = function(e) {
                console.log("Connection established!");
            };

            conn.onmessage = function(e) {
                console.log('ada message');
                console.log(e.data);
            };
            conn.onerror = function(e) {
               console.log("WebSocket Error: " , e);
               //Custom function for handling errors
               //handleErrors(e);
            };
            function sendMessage(){
                    var message = document.getElementById("pesan").value;
                    conn.send(message);
                    console.log('Sending message: ' + message);
            };

And here is the PHP code (I got this from Ratchet docs):

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

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})
";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "
"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected
";
    }

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

        $conn->close();
    }
}
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划