2

I am trying to implement Ratchet Socket Hello word on my local machine every things works perfectly but on vps centos server when i run server service with

ssh Command : php chat-server.php

it start Listening on port correctly(i can see on linux that port is Listening ) , but when i open "clint.html" page the method onopen never fire but server says

new client connected !

and after 2 minute it says

client disconnected

if i send message to client it will take 2 minutes then client will receive it, but it will disconnect again it seems to me that there is not stable connection between server and client. every time when i check websocket.readyState its not equal to 1
i disabled firewall and any security on vps server but still have this problem. i have to mention that normal php socket functions works without problem because i could test it and every thing works but about ratchet it seems it hang on onopen method.

  • port 9091 is open
  • firewall is disabled
  • abrandao.com/2013/06/websockets-html5-php/ works whiteout problem and can send and receive message from clients
  • BUT Ratchet has problem onconnect


    chat-server.php :

    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    use MyApp\Chat;
    require dirname(__DIR__) . '/vendor/autoload.php';
    
    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new Chat()
            )
        ),
        9091
    );
    echo date("Y-m-d H:i:s")." chat-server Started on port 9091 \n";
    $server->run();
    

    Chat.php class :

    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) {        
        $this->clients->attach($conn);
        echo date("Y-m-d H:i:s")." New connection! ({$conn->resourceId})\n";        
        $conn->send("Hello {$conn->resourceId} from server at : ".date("Y-m-d H:i:s"));
        echo date("Y-m-d H:i:s")." Hello Sent to  ({$conn->resourceId})\n"; 
    
    }
    
    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf(date("Y-m-d H:i:s").'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) {                
                $client->send($msg);
            }
        }
    }
    
    public function onClose(ConnectionInterface $conn) {        
        $this->clients->detach($conn);
        echo date("Y-m-d H:i:s")." Connection {$conn->resourceId} has disconnected\n";
    }
    
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo date("Y-m-d H:i:s")." An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
    }
    

    client html :

    $(document).ready(function(){  
       connect();  
    });
    
    var wsUri = "ws://myserverdomain.comOrIP:9091";   
    
    
     function connect() {
      var ws = new WebSocket(wsUri);
    
      ws.onopen = function() {
        var now = new Date();
        console.log(now + ' Connected to '+wsUri);        
      };
    
      ws.onmessage = function(e) {
          var now = new Date();
        console.log(now + ' Message Recived From Server :', e.data);
      };
    
      ws.onclose = function(e) {
          var now = new Date();
        console.log(now +' Socket is closed. Reconnect will be attempted in 1 second.', e.reason);
        setTimeout(function() {
          connect();
        }, 1000)
      };
    
      ws.onerror = function(err) {
          var now = new Date();
        console.error(now + ' Socket encountered error: ', err.message, 'Closing socket')
        console.error(err)
        ws.close()
      };
    } 
    
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
Sam Rad
  • 451
  • 2
  • 5
  • 13
  • Could you add what webserver you have running, nginx/apache/...? Since the connection closing after 2 minutes is probeably about those only being set to keep connections alive for 120 seconds – mitchken May 16 '16 at 18:05
  • @mitchken Its Apache. and i changed max_timout to 1000 It does not matter .i changed Keep-Alive Timeout , ... default_socket_timeout on php.ini and i tested alot of things . – Sam Rad May 16 '16 at 19:17
  • @mitchken Its Apache running on centos linux. and i changed max_timeout to 1000 It does not matter .i changed Keep-Alive Timeout , ... default_socket_timeout on php.ini and i tested a lot of things . its interesting for me why normal php socket works without problem but ratchet not working. i had to write my own websocket server from scratch :( and now its running well . but did any one could run ratchet hello world simple app on linux before ? i think there must be a bug in ratchet script – Sam Rad May 16 '16 at 19:26
  • 1
    I am running a ratchet PHP server with over 5K concurrrent connections. I myself started by testing the hello world as well and it worked for me – mitchken May 16 '16 at 19:27

0 Answers0