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

How to listen event onOpen in server? #5

Closed
tranphuoctien opened this issue Sep 16, 2017 · 11 comments
Closed

How to listen event onOpen in server? #5

tranphuoctien opened this issue Sep 16, 2017 · 11 comments

Comments

@tranphuoctien
Copy link

How to listen event onOpen in server? Because some time i need to save resourceId or somethings. Please help me ?

public function onOpen(ConnectionInterface $conn){
}

Where we can listen this event?

@alcalyn
Copy link
Member

alcalyn commented Sep 16, 2017

I'm implementing this method in Eole\Sandstone\Websocket\Application to handle authentication.

But this is true, I never met this use case, so actually it's hard to do it as is, because the ComponentInterface, which implements the onOpen method, is encapsuled inside Eole\Sandstone\Websocket\Application.

I think we should be able to listen to an onOpen event that I'll dispatch.

So if you could do something like this:

$dispatcher->addListener(ServerEvent::ON_OPEN, function (ConnectionInterface $conn) {
    // resourceId or somethings
});

Could it be suffisant ?

@alcalyn
Copy link
Member

alcalyn commented Sep 16, 2017

I could test your use case and it works:

[info] Initialization... []
[info] Bind websocket server {"bind":"0.0.0.0","port":8482}
[info] Bind push server {"bind":"0.0.0.0","host":"localhost","port":5555}
ON OPEN <= My event listener is called
[info] Connection event {"event":"open"}
[info] Authentication... []
[info] Anonymous connection []

You can test it by:

  • In composer, require this dev version:
    "require": {
        "eole/sandstone": "dev-feature/connection-events@dev",
    }
  • composer update
  • Listen to the ON_OPEN event:
use Eole\Sandstone\Websocket\Event\ConnectionEvent;

$this['dispatcher']->addListener(ConnectionEvent::ON_OPEN, function (ConnectionEvent $event) {
    $conn = $event->getConn();
});

Or, if you get the "dispatcher service is frozen" error, do:

$this->on(ConnectionEvent::ON_OPEN, function (ConnectionEvent $event) {
    $conn = $event->getConn();
});

If all is ok for you, I'll integrate all connection events in Sandstone.

@tranphuoctien
Copy link
Author

Hi @alcalyn
Please merge to master! I think if add it then it will be great event!!!

@alcalyn
Copy link
Member

alcalyn commented Sep 18, 2017

Okey, just wanted to be sure that, this way, it covers your use case.

I'll disptach all other events (onClose, onSubscribe, onError, ...) and release it.

Until next release, onOpen event is testable in dev-feature/connection-events@dev version.

@tranphuoctien
Copy link
Author

Thank @alcalyn, I will test on this branch too.

@alcalyn
Copy link
Member

alcalyn commented Sep 18, 2017

@tranphuoctien I opened a PR where we can listen to onOpen event: #6

I let you test and let me know if it fits your need.

@tranphuoctien
Copy link
Author

tranphuoctien commented Sep 19, 2017

I have been tested on this branch. It's working @alcalyn with both:

 $this['dispatcher']->addListener(ConnectionEvent::ON_OPEN, function (ConnectionEvent $event) {
     $conn = $event->getConn();
 });
 
 $this->on(ConnectionEvent::ON_OPEN, function (ConnectionEvent $event) {
     $conn = $event->getConn();
 });

I got it!
So can you upgrade ratchet to 0.4 too?

@alcalyn
Copy link
Member

alcalyn commented Sep 19, 2017

Good, I'll release it soon.

I already upgraded to Ratchet 0.4 (in a branch) in order to try a wss: connection, but I'll release the upgrade only.

@alcalyn
Copy link
Member

alcalyn commented Sep 19, 2017

(Oh, I just remembered that upgrading to Ratchet 0.4 will probably bump Sandstone to v2.0.0)

@tranphuoctien
Copy link
Author

Thank you @alcalyn !!!

@alcalyn
Copy link
Member

alcalyn commented Sep 19, 2017

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

No branches or pull requests

2 participants