在客户端断开连接之前,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();
    }
}
dtvdz911959
dtvdz911959 我从控制台调用它。使用chrome浏览器。
接近 2 年之前 回复
drn34916
drn34916 在您发送的代码段中,没有显式调用sendMessage函数的地方......有吗?
接近 2 年之前 回复
dongping8572
dongping8572 从我的本地计算机(Windows10)。websocket(websocket.develop.local)使用Centos7在虚拟盒子上运行。
接近 2 年之前 回复
douye2020
douye2020 你在哪里调用sendMessage函数?
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问