walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
http://www.workerman.net
MIT License
11.03k stars 2.25k forks source link

Pass user_id to the server when connecting. #956

Open ExchangeAssets opened 9 months ago

ExchangeAssets commented 9 months ago

Is there a way on the client side to transfer user_id (user ID registered on the site) and referrer (address of the page from which the connection was made) to the server, so that each connection can be checked for user_id and referrer?

ExchangeAssets commented 9 months ago

I use: $channel_server = new Channel\Server('0.0.0.0', 2347);

it sends messages to all users, as if I need to send messages only to users who are connected from the page mysite.com/page_1, while not sending messages to users connected from mysite.com/page_2.

It should roughly look like this:

foreach($worker->connections as $connection) {
    if($connection->get('page') != 'page_1') continue;
    $connection->send($data);
}

but how to pass 'page' in $connection when connecting? Do you have an example?

walkor commented 9 months ago

Just use $connection->page;