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

Commit

Permalink
Merge branch 'master' into test_incomplete_frame_fix
Browse files Browse the repository at this point in the history
Conflicts:
	src/Devristo/Phpws/Protocol/WebSocketTransportInterface.php
  • Loading branch information
Devristo committed Feb 15, 2014
2 parents 6456d6d + c46c614 commit 2f32122
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,16 @@ $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){

$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->send("Hello world!");
});
Expand All @@ -113,7 +122,6 @@ $client->on("message", function($message) use ($client, $logger){
$client->close();
});


$client->open();
$loop->run();
```
Expand Down
50 changes: 50 additions & 0 deletions src/Devristo/Phpws/Client/Connector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Devristo\Phpws\Client;

use React\SocketClient\Connector as BaseConnector;
use React\EventLoop\LoopInterface;
use React\Dns\Resolver\Resolver;

class Connector extends BaseConnector
{

protected $contextOptions = array();

public function __construct(LoopInterface $loop, Resolver $resolver, array $contextOptions = null)
{
parent::__construct($loop, $resolver);

$contextOptions = null === $contextOptions ? array() : $contextOptions;
$this->contextOptions = $contextOptions;
}

protected function createStreamContext()
{
return stream_context_create($this->contextOptions);
}

public function createSocketForAddress($address, $port)
{
$url = $this->getSocketUrl($address, $port);

$socket = stream_socket_client($url, $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT, $this->createStreamContext());

if (!$socket) {
return When::reject(new \RuntimeException(
sprintf("connection to %s:%d failed: %s", $address, $port, $errstr),
$errno
));
}

stream_set_blocking($socket, 0);

// wait for connection

return $this
->waitForStreamOnce($socket)
->then(array($this, 'checkConnectedSocket'))
->then(array($this, 'handleConnectedSocket'));
}

}
9 changes: 6 additions & 3 deletions src/Devristo/Phpws/Client/WebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ class WebSocket extends EventEmitter
protected $logger;

protected $isClosing = false;

protected $streamOptions = null;

public function __construct($url, LoopInterface $loop, LoggerInterface $logger)
public function __construct($url, LoopInterface $loop, LoggerInterface $logger, array $streamOptions = null)
{
$this->logger = $logger;
$this->loop = $loop;
$this->streamOptions = $streamOptions;
$parts = parse_url($url);

$this->url = $url;
Expand All @@ -68,7 +71,6 @@ public function __construct($url, LoopInterface $loop, LoggerInterface $logger)
$this->dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);
}


public function open($timeOut=null)
{
/**
Expand All @@ -81,7 +83,8 @@ public function open($timeOut=null)
$isSecured = 'wss' === $uri->getScheme();
$defaultPort = $isSecured ? 443 : 80;

$connector = new \React\SocketClient\Connector($this->loop, $this->dns);
$connector = new Connector($this->loop, $this->dns, $this->streamOptions);

if ($isSecured) {
$connector = new \React\SocketClient\SecureConnector($connector, $this->loop);
}
Expand Down
6 changes: 0 additions & 6 deletions src/Devristo/Phpws/Messaging/WebSocketMessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ public function getFrames();
*/
public function setData($data);

/**
* Retrieve the body of the message
* @return string
*/
public function getData();

/**
* Create a new message
* @param string $data Content of the message to be created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public function close();
public function setData($key, $value);

public function getData($key);
}
}

0 comments on commit 2f32122

Please sign in to comment.