Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RatChet PHP-websocket的问题_Php_Websocket - Fatal编程技术网

RatChet PHP-websocket的问题

RatChet PHP-websocket的问题,php,websocket,Php,Websocket,我用Ratchet做了一个webchat,它在我的计算机上运行得很好,如果我用命令行启动脚本,它就会运行,如果我在另一个浏览器中打开聊天的本地主机,它就会运行得很好,我可以和我的另一个浏览器聊天,然后,我做了,我感到很奇怪,但我尝试部署我的聊天,并与我的朋友分享,看看它是否真的对每个人都有效,而不是对每个人都有效,只发送消息,没有人能看到,因此,我不知道websocket是否真的有效,或者问题是否出在服务器主机上,因为我使用000webhost,使用websocket可能不太好 来源:chat

我用Ratchet做了一个webchat,它在我的计算机上运行得很好,如果我用命令行启动脚本,它就会运行,如果我在另一个浏览器中打开聊天的本地主机,它就会运行得很好,我可以和我的另一个浏览器聊天,然后,我做了,我感到很奇怪,但我尝试部署我的聊天,并与我的朋友分享,看看它是否真的对每个人都有效,而不是对每个人都有效,只发送消息,没有人能看到,因此,我不知道websocket是否真的有效,或者问题是否出在服务器主机上,因为我使用000webhost,使用websocket可能不太好

来源: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();
嗯,我只发送了这些来源,因为在我看来这很重要,请帮忙

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