Giter Site home page Giter Site logo

Comments (9)

sirn-se avatar sirn-se commented on June 27, 2024

Second parameter in send() is opcode. When sending ping, the text message doesn't matter.

$client->send('Ping message', 'ping');

from websocket-php.

chrispri avatar chrispri commented on June 27, 2024

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.

sirn-se avatar sirn-se commented on June 27, 2024

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.

chrispri avatar chrispri commented on June 27, 2024

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.

sirn-se avatar sirn-se commented on June 27, 2024

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.

apeman76 avatar apeman76 commented on June 27, 2024

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.

chrispri avatar chrispri commented on June 27, 2024

@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.

marcroberts avatar marcroberts commented on June 27, 2024

I've just opened a PR that helps with this - #140

from websocket-php.

ADzaka avatar ADzaka commented on June 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.