heart break
#10
-
Can the client monitor messages and send heartbeats automatically at the same time? |
Beta Was this translation helpful? Give feedback.
Answered by
sirn-se
Oct 30, 2023
Replies: 1 comment
-
Yes, by using the subscription flow with listeners. Use Something like this; $client = new WebSocket\Client("ws://echo.websocket.org/");
$client
// Add standard middlewares
->addMiddleware(new WebSocket\Middleware\CloseHandler())
->addMiddleware(new WebSocket\Middleware\PingResponder())
// Listen to incoming messages
->onText(function (WebSocket\Client $client, WebSocket\Connection $connection, WebSocket\Message\Message $message) {
// Act on incoming message
})
->onBinary(function (WebSocket\Client $client, WebSocket\Connection $connection, WebSocket\Message\Message $message) {
// Act on incoming message
})
// Called after every listener pass
->onTick(function (WebSocket\Client $client) {
$client->ping();
});
$client->text("Initial message to server typically required");
$client->start(); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sirn-se
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, by using the subscription flow with listeners.
Use
onText()
and possiblyonBinary()
to handle incoming messages. Then useonTick()
to push ping messages to server at interval.Something like this;