Skip to content
This repository was archived by the owner on Sep 7, 2019. It is now read-only.

Conversation

sfranzyshen
Copy link
Contributor

Added the public function getMessages() so that one can test the status of the _messages array without the need to call the readMessage() function (blocking loop).

Instead of:

// Wait for an incoming message
$msg = $client->readMessage(); // blocking loop

New usage:

if($client->getMessages()) // non-blocking
___$msg = $client->readMessage();

Added the public function getMessages() so that one can test the status of the _messages array without needing to enter the readMessage() function (blocking loop).

Instead of:

// Wait for an incoming message
$msg = $client->readMessage(); // blocking loop

New usage:

if($client->getMessages()) // non-blocking
	 $msg = $client->readMessage();
@sfranzyshen
Copy link
Contributor Author

Hi Chris!

Yes, I made a second pull request with the function named hasMessages(). I'm actually returning the count of waiting messages instead of boolean ... but it can be used in the same way ... if(hasMessages())

@Devristo
Copy link
Owner

Yeah I see now, I must have been blind yesterday. To me getMessages sounds like you would get the messages instead of a count. Thats why I merged your other PR, its the same function with a different name.

@Parmee
Copy link

Parmee commented Nov 25, 2013

I'm facing an issue with this new feature you have added - it seems hasMessages() does not update unless you have previously executed readMessage(), which means you will need to run readMessage() in order to get the latest total in hasMessages() which causes the blocking issue once again, which is not ideal as I wish to send a message to the server every 5 seconds whether or not a message has been received.

When I have run the script with just detecting for hasMessages() it simply does not process any messages received from the server.

Thanks!

@Devristo
Copy link
Owner

You are right. The client will always be blocking as long as there is nothing like an event loop. I am planning a clean up of phpws which gets rid of the Hixie stuff and includes some kind of event loop in the client. Such that events are triggered when messages are received. This will solve lots of problems and make the client actually useful.

@Devristo
Copy link
Owner

For a more useful, but very 'alpha' client see the 'react' branch. It is based on reactphp and the psr0 branch. This means it is quite different than the master branch. An client example usage can be seen in tests/echo_client.php. Which contains the following at this time:

require_once(__DIR__."/../vendor/autoload.php");

$loop = \React\EventLoop\Factory::create();


$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);

$client = new \Devristo\Phpws\Client\WebSocket("ws://echo.websocket.org/?encoding=text", $loop, $logger);
$client->on("connected", function($headers) use ($logger, $client){
    $logger->notice("Connected!");
    $client->send("Hello world!");
});

$client->on("message", function($message) use ($client, $logger){
    $logger->notice("Got message: ".$message->getData());
    $client->close();
});


$client->open();
$loop->run();

@Devristo
Copy link
Owner

Will close this PR now since the client (and server) has been overhauled to use Async I/O using https://github.com/reactphp/react/ .

@Devristo Devristo closed this Nov 28, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants