Description
Hello,
I ran into an interesting issue with this library when attempting to use Devristo/phpws and its builtin websocket client. When data is read from a stream using the Stream class' handleData function, reads seem to be blocking even though stream_set_blocking is called in __construct. I found that replacing line 121's fread
with a stream_get_contents
resolved my blocking reads issue. I feel like this class should be using the stream_
functions anyways - but it seems like a mix of f*
and stream_
functions are used.
Is there a possibility a fix for this can be issued?
The only way I know to reproduce this involves a lot of steps and a specific product (Slack) and protocol (Hybi Websockets). I have tested that this issue exists on multiple boxes using PHP 5.5. I'll outline the steps to reproduce..
- Sign up for a free Slack team here: https://slack.com/
- Use the slack API doc site to 'test' the
rtm.start
call. This will give you a Websocket URL to connect to (must be used within 30 seconds, FYI). It's at the very bottom of the output. https://api.slack.com/methods/rtm.start/test - Connect using Devristo/phpws using the URL you got in step 2.
require_once("vendor/autoload.php"); // Composer autoloader
$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("wss_url_here", $loop, $logger);
$client->on("request", function($headers) use ($logger){
$logger->notice("Request object created!");
});
$client->on("handshake", function() use ($logger) {
$logger->notice("Handshake received!");
});
$client->on("connect", function($headers) use ($logger, $client){
$logger->notice("Connected!");
});
$client->on("message", function($message) use ($client, $logger){
$logger->notice("Got message: ".$message->getData());
});
$client->open();
$loop->run();
- Type messages on the Slack web client from one or more users. Note that your PHP app will always be one event behind because it it blocking until the next read comes in.
Again, I know this is kind of a weird example, but it's the only thing I've been using this lib for and I don't have another websocket service I can test. It may be related to how the websocket clients end their frames/lines and how fread expects them to end... I'm not completely sure.
Let me know if you need more information on this. I will be happy to provide what I can.