2

我正在尝试在我的本地机器上实现 Ratchet Socket Hello 字,一切正常,但在 vps centos 服务器上运行服务器服务时

ssh Command : php chat-server.php

它开始正确侦听端口(我可以在 linux 上看到该端口正在侦听),但是当我打开“clint.html”页面时,该方法onopen永远不会触发,但服务器说

新客户端连接!

2分钟后它说

客户端断开连接

如果我向客户端发送消息,它将需要 2 分钟,然后客户端会收到它,但它会再次断开连接,在我看来,服务器和客户端之间的连接不稳定。每次当我检查 websocket.readyState 它不等于 1
我禁用防火墙和 vps 服务器上的任何安全但仍然有这个问题。我不得不提一下,正常的 php 套接字函数可以正常工作,因为我可以对其进行测试,并且一切正常 ,但是关于棘轮,它似乎挂在onopen方法上。

  • 端口 9091 已打开
  • 防火墙被禁用
  • abrandao.com/2013/06/websockets-html5-php/ 工作白化问题,可以发送和接收来自客户端的消息
  • 但是棘轮在连接上有问题


    聊天服务器.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 类:

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

    客户端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()
      };
    } 
    
4

0 回答 0