简体   繁体   中英

RatChet PHP - Problems with websocket

I did a webchat using Ratchet, it works perfectly on my computer, if I start the script by using command line, it runs, and if I open the localhost of the chat in another browser, it works perfectly, I can talk with my other browser, then, I did it, I felt wonder, but I tried to make deploy of my chat, and share with my friends to see if it is really working for everyone, and isn't working to everyone, only send the message and nobody can see, so, I don't know if the websocket is really working or if the problem is the server host, because I use 000webhost, maybe it isn't so good using websockets.

Source: chat-server.php

<?php
    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    use Ricardo\Socket\Chat;

    require 'vendor/autoload.php';

        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    new Chat()
                )
            ),
            8080
        );

        $server->run();

Source: chat.php(/lib/Ricardo/Socket)

<?php
    namespace Ricardo\Socket;

    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})\n";
        }
    
        public function onMessage(ConnectionInterface $from, $msg) {
            //$numRecv = count($this->clients) - 1;
            //echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            //    , $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\n";
        }
    
        public function onError(ConnectionInterface $conn, \Exception $e) {
            echo "An error has occurred: {$e->getMessage()}\n";
    
            $conn->close();
        }
    }

Source: script.js

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

conn.onmessage = function(e) {
//    console.log(e.data);
    showMessages('other', e.data);
};

//conn.send('Hello World!');
///////////////////////////////////////////////
var form1 = document.getElementById('chat');
var inp_message = document.getElementById('message');
var inp_name = document.getElementById('name');
var btn_env = document.getElementById('send');
var area_content = document.getElementById('content');

btn_env.addEventListener('click', function(){
    if (inp_message.value != '') {
        var msg = {'name': inp_name.value, 'msg': inp_message.value};
        msg = JSON.stringify(msg);

        conn.send(msg);

        showMessages('me', msg);

        inp_message.value = '';
    }
});


function showMessages(how, data) {
    data = JSON.parse(data);

    console.log(data);

    if (how == 'me') {
        var img_src = "chat.png";
    } else if (how == 'other') {
        var img_src = "chat-1.png";
    }

    var div = document.createElement('div');
    div.setAttribute('class', how);

    var img = document.createElement('img');
    img.setAttribute('src', img_src);

    var div_txt = document.createElement('div');
    div_txt.setAttribute('class', 'text');

    var h5 = document.createElement('h5');
    h5.textContent = data.name;

    var p = document.createElement('p');
    p.textContent = data.msg;

    div_txt.appendChild(h5);
    div_txt.appendChild(p);

    div.appendChild(img);
    div.appendChild(div_txt);

    area_content.appendChild(div);
}

Well, I sent only these sources, because in my opinion that's important, help, please!

Apparently, ratchet doesn't work on 000webhost, as it requires shell access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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