如何从websocket(PHP)中识别发件人

如何从websocket(PHP)中识别发件人,websocket,identify,Websocket,Identify,我尝试用WebSocket(JS+PHP)构建一个网站,我想从发送者那里获得套接字。例如,如果客户端“a”向我的服务器发送消息,我如何从客户端“a”获取套接字。 我在谷歌上搜索过,但唯一发现的是socket_select等待通信并返回更改的socket编号,但我无法获取socket本身 下面是我的PHP代码 <?php $host = 'localhost'; //host $port = '9000'; //port $null = NULL; //null var //Create

我尝试用WebSocket(JS+PHP)构建一个网站,我想从发送者那里获得套接字。例如,如果客户端“a”向我的服务器发送消息,我如何从客户端“a”获取套接字。 我在谷歌上搜索过,但唯一发现的是socket_select等待通信并返回更改的socket编号,但我无法获取socket本身

下面是我的PHP代码

<?php

$host = 'localhost'; //host
$port = '9000'; //port
$null = NULL; //null var
//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

//bind socket to specified host
socket_bind($socket, 0, $port);

//listen to port
socket_listen($socket);

//create & add listning socket to the list
$sockets = array($socket);
$users = array();

$validActions = array('init');

//start endless loop, so that our script doesn't stop
while (true) {
    //manage multipal connections
    $changed = $sockets;
    //returns the socket resources in $changed array
    socket_select($changed, $null, $null, 0, 10);

    //check for new socket
    if (in_array($socket, $changed)) {
        $socket_new = socket_accept($socket); //accept new socket
        $sockets[] = $socket_new; //add socket to client array

        $header = socket_read($socket_new, 1024); //read data sent by the socket
        perform_handshaking($header, $socket_new, $host, $port); //perform websocket handshake

        socket_getpeername($socket_new, $ip); //get ip address of connected socket
        $response = mask(json_encode(array('type' => 'system', 'message' => $ip . ' connected'))); //prepare json data
        send_message($response); //notify all users about new connection
        //make room for new socket
        $found_socket = array_search($socket, $changed);
        unset($changed[$found_socket]);
    }

    $received_text = unmask($buf); //unmask data
    $received_msg = json_decode($received_text); //json decode 


    //check for any incomming data
    while (socket_recv($changed_socket, $buf, 1024, 0) >= 1) {
        $received_text = unmask($buf); //unmask data
        $tst_msg = json_decode($received_text); //json decode
        // I HAVE TO GIVE $socket FROM THE CLIENT TO THE process() FUNCTION
        process($received_msg);
        ... MORE CODE ....
    }
}
// close the listening socket
socket_close($sock);

我想我必须使用socket\u select,我已经阅读了文档,但是我仍然没有找到从消息发送者那里获取socket的正确方法

我发现我可以在与socket_accept的连接中从客户端获取套接字,但是之后有没有办法从发送方获取套接字呢?