1 | <?php |
||
26 | class Connection extends AbstractConnection |
||
27 | { |
||
28 | 9 | public function __construct( |
|
40 | |||
41 | 9 | private function initListeners() |
|
42 | { |
||
43 | $this->stream->on('data', function ($data) { |
||
44 | 8 | $this->processData($data); |
|
45 | 9 | }); |
|
46 | $this->stream->once('end', function() { |
||
47 | 3 | if (!$this->handshakeDone) { |
|
48 | 1 | $this->logger->info('Disconnected but websocket didn\'t start.'); |
|
49 | 1 | return; |
|
50 | } |
||
51 | 2 | $this->getHandler()->onDisconnect($this); |
|
52 | 9 | }); |
|
53 | $this->stream->on('error', function ($data) { |
||
54 | $this->error($data); |
||
55 | 9 | }); |
|
56 | 9 | } |
|
57 | |||
58 | 8 | private function processData($data) |
|
77 | |||
78 | /** |
||
79 | * This method build a message and buffer data in case of incomplete data. |
||
80 | * |
||
81 | * @param string $data |
||
82 | */ |
||
83 | 2 | protected function processMessage(string $data) |
|
84 | { |
||
85 | // It may be a timeout going (we were waiting for data), let's clear it. |
||
86 | 2 | if ($this->timeout !== null) { |
|
87 | $this->loop->cancelTimer($this->timeout); |
||
88 | $this->timeout = null; |
||
89 | } |
||
90 | |||
91 | 2 | foreach ($this->messageProcessor->onData($data, $this->stream, $this->currentMessage) as $message) { |
|
92 | 2 | $this->currentMessage = $message; |
|
93 | 2 | if ($this->currentMessage->isComplete()) { |
|
94 | // Sending the message through the woketo API. |
||
95 | 2 | switch($this->currentMessage->getOpcode()) { |
|
96 | case Frame::OP_TEXT: |
||
97 | 1 | $this->getHandler()->onMessage($this->currentMessage->getContent(), $this); |
|
98 | 1 | break; |
|
99 | case Frame::OP_BINARY: |
||
100 | 1 | $this->getHandler()->onBinary($this->currentMessage->getContent(), $this); |
|
101 | 1 | break; |
|
102 | } |
||
103 | 2 | $this->currentMessage = null; |
|
104 | |||
105 | } else { |
||
106 | // We wait for more data so we start a timeout. |
||
107 | $this->timeout = $this->loop->addTimer(Connection::DEFAULT_TIMEOUT, function () { |
||
108 | $this->logger->notice('Connection to ' . $this->getIp() . ' timed out.'); |
||
109 | $this->messageProcessor->timeout($this->stream); |
||
110 | }); |
||
111 | } |
||
112 | } |
||
113 | 2 | } |
|
114 | |||
115 | public function write($frame, int $opCode = Frame::OP_TEXT) |
||
123 | |||
124 | /** |
||
125 | * @param mixed $data |
||
126 | */ |
||
127 | protected function error($data) |
||
133 | |||
134 | /** |
||
135 | * If it's a new client, we need to make some special actions named the handshake. |
||
136 | * |
||
137 | * @param string $data |
||
138 | */ |
||
139 | 8 | protected function processHandshake(string $data) |
|
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | 5 | public function getIp() |
|
163 | } |
||
164 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: