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() }; }