swooletw / laravel-swoole

High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
MIT License
4.04k stars 390 forks source link

How to get websocket server instance and send some message to fd=1? #445

Closed jianhuawang closed 4 years ago

jianhuawang commented 4 years ago

PHP Version : 7.3.16
Swoole Version : 4.4.17
Laravel Version : 8.10.0

swooletw/laravel-swoole v2.6.68

I have run websocket server successfuly. now this websocket server received a POST request, after receive the post data, server should send some message throught websocket channel. I have a swoole table saved the fds information. for example: fd=1.

I need help: how to sent some message to fd ( websocket client).

Arkanius commented 4 years ago

Do you need to send a broadcast message?

jianhuawang commented 4 years ago

@Arkanius Broadcast message is not required. Maybe needed in feture, but now I want to send message by fd through websocket. Do you have some suggestion?

jianhuawang commented 4 years ago

I have find the way.

use SwooleTW\Http\Websocket\Facades\Websocket;

$fd= $mySwooleTable->getFdByUserId($user->id); //$user is the message receiver I want to notice.

$websocket= Websocket::getFacadeRoot();
$websocket->setSender($fd); // this step is not required.

$websocket->to($fd)->emit($event, $data );
Arkanius commented 4 years ago

Nice job @jianhuawang ! Sorry, I couldn't see this issue yesterday.