PHP Ratchet Websocket 服务器与 Apache2 不工作

问题描述 投票:0回答:0

我有一个用php编写的websocket服务器,它是Ratchet websocket服务器。 我为 apache 启用了 wstunnel。我还在我的网站配置中添加了 ProxyPass

这是我网站的 apache 配置。

 <VirtualHost *:443>
 DocumentRoot /var/www/mywebsite/pav/
 ServerName www.mywebsite.com
 ServerAlias mywebsite.com

 RewriteEngine on
 ProxyPass /wss2/ ws://www.mywebsite.com:8080

 
 <Directory /var/www/mywebsite/pav/>
 DirectoryIndex index.php
 AllowOverride All
 Order allow,deny
 Allow from all
 </Directory>
 </VirtualHost>

我的网站是用php和JS编写的。 在 javascript 中,我用来连接到服务器的脚本是

 var socket = new WebSocket('wss://www.mywebsite.com:8080');

我使用检查检查了我的浏览器(Firefox),并在控制台中显示: Firefox 无法与 wss://www.xpatvoice.com:8080/ 的服务器建立连接。

我尝试过其他浏览器并得到相同的结果。

问题是我不知道如何判断是什么原因导致它失败, 我的意思是任何错误代码或错误日志来诊断问题。

我也尝试过


 var socket = new WebSocket('wss://www.mywebsite.com/wss2/:8080');

这是聊天套接字类

namespace MyApp;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;


class Socket implements MessageComponentInterface {

    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
        $this->users = [];
        $this->inqs = [];
    }

    public function onOpen(ConnectionInterface $conn) {

        // Store the new connection in $this->clients
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
        //echo $conn->httpRequest->headers["X-Forwarded-For"][0];
                
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        
        $data = json_decode($msg);
        $userinfo = json_decode($data->userinfo,true);

       if(isset($userinfo["userId"]) && $data->action == "userLogin"){
                $userID = $userinfo["userId"];
                $this->users[$userID]["conn"] = $from;
                $this->users[$userID]["role"] = $userinfo["role"]; 
                echo "attached user $userID\n";
           
           // NOTIFY INQUISITORS OF ADMIN LOGIN
                if($userinfo["role"] < 3){
                    foreach($this->inqs as $inq => $client){
                        $message["action"] = "adminLogon";
                        $client->send(json_encode($message));
                    }
                 } 
       }
        
        if(isset($userinfo["userId"]) && $data->action == "adminReply"){
            
            $this->inqs[$data->to]->send($msg);
            
        }
        
           if(isset($userinfo["tempuser"]) && $data->action == "enquiry"){
                $userID = $userinfo["tempuser"];
                $this->inqs[$userID] = $from;
                echo "attached inquisitor $userID\n";
          }
        
        if(isset($userinfo["tempuser"])){
            
            foreach($this->users as $userid => $user){
                if($user["role"] < 3){
                    $user["conn"]->send($msg);
                }
            }
            echo "message from inquisitor\n";
         }
        

    }

    public function onClose(ConnectionInterface $conn) {

      $this->clients->detach($conn);

    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}
php linux websocket apache2 ratchet
© www.soinside.com 2019 - 2024. All rights reserved.