Comments (9)
Second parameter in send() is opcode. When sending ping
, the text message doesn't matter.
$client->send('Ping message', 'ping');
from websocket-php.
Second parameter in send() is opcode. When sending
ping
, the text message doesn't matter.$client->send('Ping message', 'ping');
Thanks for answer.
But this isn´t possible when I am receiving data without async? Am I wrong?
from websocket-php.
Correct, the receive()
operation is blocking. So if you are waiting for input but the server doesn't write anything for 200 seconds the connection will close.
from websocket-php.
Thats bad.
Do you have any ideas how i can get this work?
Here is the manual of Gardena/Husquvarna: https://developer.husqvarnagroup.cloud/apis/GARDENA+smart+system+API#/readme
And thats the text part which makes problems:
Make a WebSocket connection using the obtained URL. Since the WebSocket is closed automatically after 300 seconds of inactivity, we recommend sending ping messages every 150 seconds to keep the connection open.
from websocket-php.
Only thing to do is to expect socket to close and have it re-connect. As that API allows http-requests as well it should not be a problem.
I will consider this use case for further development, though.
from websocket-php.
What about this:
$client = new WebSocket\Client("wss://URL", [
'timeout' => 5
]);
$lastSend = round(microtime(true) * 1000);
while (true) {
try {
$time = round(microtime(true) * 1000);
if ($time > $lastSend + 4.9 * 1000) {
$lastSend = round(microtime(true) * 1000);
$client->send('ping', 'ping');
}
$data = $client->receive();
if ($data) {
print_r($data);
}
} catch (\WebSocket\ConnectionException $e) {
print_r($e);
}
}
$client->close();```
from websocket-php.
@mrsnut
I tried a similar code but this can't work because the server doesn't send data the most time.
So the while loop stops at "$data = $client->receive();" until the timeout closes the connection.
from websocket-php.
I've just opened a PR that helps with this - #140
from websocket-php.
I found an easier method. When you set up the client, set a timeout for the connection (one of the options parameters), like this:
$client = new \WebSocket\Client(getenv('WS_REALTIME_URL_SPOT'), ['timeout' => 20]);
After that, in the catch where all the websocket errors are caught, you will get a timeout after not receiving data, every 20 seconds. Use this timeout to send a heartbeat.
} catch (\WebSocket\ConnectionException $e) {
$dateStartPlusOneMinute = Carbon::now()->addSeconds(70);
if ($e->getMessage() == "Client read timeout") {
$client->text('{"op":"ping"}');
} else {
Log::info("Foutmelding: ".$e->getMessage());
}
}
from websocket-php.
Related Issues (20)
- Maximum execution time on Header block HOT 3
- Mozilla is not recognized as an internal or external command HOT 1
- Connection with WSS HOT 3
- How can I use this package with laravel? HOT 8
- Update to last version and websocket wss://ws-api.exmo.com:443/v1/public do not work HOT 3
- stream_socket_client(): Unable to connect to ssl HOT 1
- Client Ping/Pong broken in 1.6 HOT 5
- The current version only works on PHP8 HOT 1
- How to install this? HOT 6
- Looping receive() is not working HOT 3
- How to set proxy IP HOT 3
- failed to connect wss HOT 1
- Composer not load method HOT 1
- NOTE: TexTalk replaced by sirn-se/websocket-php
- Upgrade psr/http-message to ^1.1 || ^2.0 HOT 3
- heart break
- permessage-deflate problem HOT 1
- sometimes connection refused, but ends in fatal error HOT 1
- Does this socket work on https?
- 初次使用后的一些体验与几点疑惑,期望得到大佬们的一些帮助,谢谢!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from websocket-php.