How to listen event onOpen in server?
tranphuoctien opened this issue · comments
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?
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 ?
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.
Hi @alcalyn
Please merge to master! I think if add it then it will be great event!!!
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 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.
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?
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.
(Oh, I just remembered that upgrading to Ratchet 0.4 will probably bump Sandstone to v2.0.0)
Reelased in 1.3.0 https://github.com/eole-io/sandstone/releases/tag/1.3.0