|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
/**
|
|
4
|
|
|
* This file is a part of Woketo package.
|
|
5
|
|
|
*
|
|
6
|
|
|
* (c) Nekland <[email protected]>
|
|
7
|
|
|
*
|
|
8
|
|
|
* For the full license, take a look to the LICENSE file
|
|
9
|
|
|
* on the root directory of this project
|
|
10
|
|
|
*/
|
|
11
|
|
|
|
|
12
|
|
|
namespace Nekland\Woketo\Client;
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
use Nekland\Woketo\Core\AbstractConnection;
|
|
16
|
|
|
use Nekland\Woketo\Exception\Http\IncompleteHttpMessageException;
|
|
17
|
|
|
use Nekland\Woketo\Exception\WebsocketException;
|
|
18
|
|
|
use Nekland\Woketo\Http\Request;
|
|
19
|
|
|
use Nekland\Woketo\Http\Response;
|
|
20
|
|
|
use Nekland\Woketo\Rfc6455\Handshake\ClientHandShake;
|
|
21
|
|
|
use Nekland\Woketo\Rfc6455\MessageProcessor;
|
|
22
|
|
|
use React\Promise\PromiseInterface;
|
|
23
|
|
|
use React\Stream\Stream;
|
|
24
|
|
|
|
|
25
|
|
|
class Connection extends AbstractConnection
|
|
26
|
|
|
{
|
|
27
|
|
|
/**
|
|
28
|
|
|
* @var Stream
|
|
29
|
|
|
*/
|
|
30
|
|
|
private $stream;
|
|
31
|
|
|
|
|
32
|
|
|
/**
|
|
33
|
|
|
* @var bool
|
|
34
|
|
|
*/
|
|
35
|
|
|
private $requestSent;
|
|
36
|
|
|
|
|
37
|
|
|
/**
|
|
38
|
|
|
* @var string
|
|
39
|
|
|
*/
|
|
40
|
|
|
private $host;
|
|
41
|
|
|
|
|
42
|
|
|
/**
|
|
43
|
|
|
* @var string
|
|
44
|
|
|
*/
|
|
45
|
|
|
private $buffer;
|
|
46
|
|
|
|
|
47
|
|
|
public function __construct(string $uri, string $host, PromiseInterface $clientPromise, MessageProcessor $messageProcessor)
|
|
48
|
|
|
{
|
|
49
|
|
|
parent::__construct($messageProcessor, new ClientHandShake());
|
|
50
|
|
|
|
|
51
|
|
|
$this->requestSent = false;
|
|
52
|
|
|
$this->uri = $uri;
|
|
53
|
|
|
$this->host = $host;
|
|
54
|
|
|
$this->buffer = '';
|
|
55
|
|
|
|
|
56
|
|
|
$clientPromise->then(function (Stream $stream) {
|
|
57
|
|
|
$this->stream = $stream;
|
|
58
|
|
|
$this->onConnection($stream);
|
|
59
|
|
|
}, function (\Exception $error){
|
|
60
|
|
|
$this->onError($error);
|
|
|
|
|
|
|
61
|
|
|
});
|
|
62
|
|
|
$this->processHandshake('');
|
|
63
|
|
|
}
|
|
64
|
|
|
|
|
65
|
|
|
private function onConnection(Stream $stream)
|
|
66
|
|
|
{
|
|
67
|
|
|
$stream->on('message', function (string $data) {
|
|
68
|
|
|
$this->onMessage($data);
|
|
69
|
|
|
});
|
|
70
|
|
|
}
|
|
71
|
|
|
|
|
72
|
|
|
protected function processHandshake(string $data)
|
|
73
|
|
|
{
|
|
74
|
|
|
// Sending initialization request
|
|
75
|
|
|
if (!$this->requestSent) {
|
|
76
|
|
|
$request = Request::createClientRequest($this->uri, $this->host);
|
|
77
|
|
|
$this->stream->write($request->getRequestAsString());
|
|
78
|
|
|
return;
|
|
79
|
|
|
}
|
|
80
|
|
|
|
|
81
|
|
|
$this->buffer .= $data;
|
|
82
|
|
|
|
|
83
|
|
|
try {
|
|
84
|
|
|
$response = Response::create($data);
|
|
85
|
|
|
} catch (IncompleteHttpMessageException $e) {
|
|
86
|
|
|
return;
|
|
87
|
|
|
}
|
|
88
|
|
|
|
|
89
|
|
|
$this->handshake->verify($response);
|
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
// Receiving switching protocol response
|
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
// Verifying response
|
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$this->handshakeDone = true;
|
|
99
|
|
|
}
|
|
100
|
|
|
|
|
101
|
|
|
protected function processMessage(string $data)
|
|
102
|
|
|
{
|
|
103
|
|
|
// TODO: Implement processMessage() method.
|
|
104
|
|
|
}
|
|
105
|
|
|
|
|
106
|
|
|
/**
|
|
107
|
|
|
* @param \Exception|string $error
|
|
108
|
|
|
*/
|
|
109
|
|
|
private function onError($error)
|
|
|
|
|
|
|
110
|
|
|
{
|
|
111
|
|
|
|
|
112
|
|
|
}
|
|
113
|
|
|
}
|
|
114
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: