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

Would you tell me How to use controller send websocket MSG #464

Open kelvin-yuang opened 3 years ago

kelvin-yuang commented 3 years ago

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 commented 3 years ago

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

Arkanius commented 3 years ago

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

kelvin-yuang commented 3 years ago

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。

Abbotton commented 3 years ago

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');
});
kelvin-yuang commented 3 years ago

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

<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use SwooleTW\Http\Websocket\Facades\Websocket; use SwooleTW\Http\Websocket\Facades\Room;

class WebSocketController extends Controller {
public function test(){ $userId = Websocket::getUserId(); Websocket::toUserId($userId)->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 commented 3 years ago

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; }

musichook commented 1 year ago

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.