laravel-swoole icon indicating copy to clipboard operation
laravel-swoole copied to clipboard

Would you tell me How to use controller send websocket MSG

Open kelvin-yuang opened this issue 4 years ago • 7 comments

Make sure you read Issues Guideline and answer these questions before submitting your issue. Thanks! (Any non-English issues will be closed immediately.)

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole) PHP 7.3.25 Swoole => enabled Version => 4.4.23

  2. Please provide your Laravel/Lumen version. Laravel8.x

  3. Which release version of this package are you using? I think is fix ok

  4. What did you do? If possible, provide a recipe for reproducing the error. i don't konw how to use controller@method to make websocket send MSG

kelvin-yuang avatar Jan 27 '21 08:01 kelvin-yuang

could i have some method like swoole websocktet can be use Curl post data to "onRequest" ?

kelvin-yuang avatar Jan 27 '21 09:01 kelvin-yuang

Hi!

I think you need to use other events, like: onStart, onOpen, etc.

Please, take a look at: https://github.com/swooletw/laravel-swoole/wiki/7.-Websocket and https://www.swoole.co.uk/docs/modules/swoole-websocket-server

Arkanius avatar Jan 27 '21 14:01 Arkanius

The first is Thank for you answer my question. but, you maybe misunderstood,I meat used Router->controller->function->sendMSG to All fd. Execution by controller to send MSG 。 like that: https://wiki.swoole.com/#/websocket_server?id=onrequest PHP can use curl function to touch websocket do something.

if use events,the first is get MSG to do ->controller->function,that is contrary. router/websocket.php->bind Controller->function。

kelvin-yuang avatar Jan 28 '21 03:01 kelvin-yuang

The document is very clear. see here

eg:

// routes/web.php
use SwooleTW\Http\Websocket\Facades\Websocket;

Route::get('test', function(){
    $userId = Websocket::getUserId();
    Websocket::toUserId($userId)->emit('message', 'hi there');
});

Abbotton avatar Jan 30 '21 07:01 Abbotton

I try it, Laravel8 show me some error: Illuminate\Contracts\Container\BindingResolutionException "Target class [swoole.websocket] does not exist.",

config/app.php have add "SwooleTW\Http\LaravelServiceProvider::class," routes/web.app have add "use SwooleTW\Http\Websocket\Facades\Websocket;"

i found querstion from: https://github.com/swooletw/laravel-swoole/issues/415 It's still unresolved

I also try make a controller files

emit('message', 'hi there'); } } still show error : Illuminate\Contracts\Container\BindingResolutionException Target class [swoole.websocket] does not exist. webscket.php is run allright , client connect also ok, just routes can't run Illuminate\Contracts\Container\BindingResolutionException ? looke like Providers register have some wrong ?

kelvin-yuang avatar Jan 31 '21 07:01 kelvin-yuang

i found some info use $userId = Websocket::getUserId();

TypeError Argument 1 passed to SwooleTW\Http\Websocket\Rooms\TableRoom::getRooms() must be of the type int, null given, called in /var/www/html/laravel8/vendor/swooletw/laravel-swoole/src/Websocket/Authenticatable.php on line 103

public function getUserId() { if (! is_null($this->userId)) { return $this->userId; } $rooms = $this->room->getRooms($this->getSender()); foreach ($rooms as $room) { if (count($explode = explode(static::USER_PREFIX, $room)) === 2) { $this->userId = $explode[1]; } } return $this->userId; }

kelvin-yuang avatar Jan 31 '21 08:01 kelvin-yuang

I try it, Laravel8 show me some error: Illuminate\Contracts\Container\BindingResolutionException "Target class [swoole.websocket] does not exist.",

config/app.php have add "SwooleTW\Http\LaravelServiceProvider::class," routes/web.app have add "use SwooleTW\Http\Websocket\Facades\Websocket;"

i found querstion from: #415 It's still unresolved

I also try make a controller files

emit('message', 'hi there'); } } still show error : Illuminate\Contracts\Container\BindingResolutionException Target class [swoole.websocket] does not exist. webscket.php is run allright , client connect also ok, just routes can't run Illuminate\Contracts\Container\BindingResolutionException ? looke like Providers register have some wrong ?

Have you resolved this issue? I met the same.

musichook avatar Aug 01 '23 10:08 musichook