2

我正在使用 PHP-Websocket 开发一个小项目。

服务器端使用此https://github.com/ghedipunk/PHP-Websockets运行

服务器端:

require "PHP-Websockets/websockets.php";

class Server extends WebSocketServer
{
    private $_connecting = 'Connecting..';
    private $_welcome = 'SOCKET SERVER!';


    protected function connected($user)
    {
        // Send welcome message to user when connected

    }    



    protected function process($user, $message)
    {
        // data sent from client
        $json = json_decode($message);

        //prepare data response to client
        $response = json_encode(array('type'=>'notify', 'message'=>'Client'.$user->id.' has sent a request.'));
        $this->send($user, $response);
    }

    protected function closed($user)
    {
        // Alert on server
        echo "User $user->id has closed the connection".PHP_EOL;
    }

    public function __destruct()
    {
        echo "Server Closed!".PHP_EOL;
    }
}

$addr = 'localhost';
$port = '2207';
$server = new Server($addr, $port);
$server->run();

客户端:

<script>
var uri = "ws://localhost:2207";
function socket_connect(){
    socket = new WebSocket(uri);
    if(!socket || socket == undefined) return false;

    socket.onopen = function(){
        console.log('Connected to Server!');
    }

    socket.onerror = function(){
        console.log('Connection Failed!');
    }

    socket.onclose = function(){
        socket_log('Connection Closed! ')
    }

    socket.onmessage = function(e){
        //var response_data = e.data;
        var msg = JSON.parse(e.data); //PHP sends Json data to client
        console.log(msg.message);
        var new_response = '<li>'+msg.message+'</li>;
        $('#response').append(new_response);
    }
}

function send_data_to_server(data){
    if(!socket || socket == undefined) return false;
    socket.send(JSON.stringify(data));
}

$(document).ready(function(){
    socket_connect();


    $('#send_request').click(function(){
        send_data_to_server({message: 'Message sent from Client'});
    });
});
</script>

<input type="button" id="send_request" value="Send Request to Server" />

<ul id="responses"></ul>

上面的代码一切正常。

当 Client1 向 Server 发送请求时,Server 会立即响应他。但是其他客户端看不到响应消息。

所以我想让它更进一步:当客户端向服务器发送请求时,服务器将响应所有客户端,以便所有客户端都能看到消息。

我怎样才能做到这一点?

提前感谢&&对不起我的英语不好!

4

2 回答 2

2

When a user connect, you need to add him to an array with every other users. When one disconnect, remove him from this array.

Then when you want to send a message to every user, iterate on the array and send the message to each connected user.

于 2014-03-04T11:06:40.747 回答
0
WebSocketServer class has WebSocketServer::$users variable.

WebSocketServer::$users如果您在函数中迭代split_packet然后调用主进程它将起作用。在最新的源代码中,请在第 405 行进行迭代。

//original
if ((preg_match('//u', $message)) || ($headers['opcode']==2)) {
        //$this->stdout("Text msg encoded UTF-8 or Binary msg\n".$message); 
        $this->process($user, $message);
      } else {
        $this->stderr("not UTF-8\n");
      }

//必须改变

if ((preg_match('//u', $message)) || ($headers['opcode']==2)) {
        //$this->stdout("Text msg encoded UTF-8 or Binary msg\n".$message); 
        foreach($this->users as $usr){
            $this->process($usr, $message);
        }
      } else {
        $this->stderr("not UTF-8\n");
      }
于 2018-04-11T10:24:23.363 回答