Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kelvin-yuang opened this issue Jan 27, 2021 · 7 comments
Open

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

kelvin-yuang opened this issue Jan 27, 2021 · 7 comments
Labels

Comments

@kelvin-yuang
Copy link

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
Copy link
Author

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

@Arkanius
Copy link
Member

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
Copy link
Author

kelvin-yuang commented Jan 28, 2021

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
Copy link

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
Copy link
Author

kelvin-yuang commented Jan 31, 2021

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 ?

@kelvin-yuang
Copy link
Author

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants