|
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\Core;
|
|
13
|
|
|
|
|
14
|
|
|
use Nekland\Woketo\Exception\WebsocketException;
|
|
15
|
|
|
use Nekland\Woketo\Message\MessageHandlerInterface;
|
|
16
|
|
|
use Nekland\Woketo\Rfc6455\Handshake\HandshakeInterface;
|
|
17
|
|
|
use Nekland\Woketo\Rfc6455\MessageProcessor;
|
|
18
|
|
|
use Psr\Log\LoggerAwareTrait;
|
|
19
|
|
|
use React\Socket\Connection;
|
|
20
|
|
|
|
|
21
|
|
|
abstract class AbstractConnection
|
|
22
|
|
|
{
|
|
23
|
|
|
use LoggerAwareTrait;
|
|
24
|
|
|
|
|
25
|
|
|
/**
|
|
26
|
|
|
* @var Connection
|
|
27
|
|
|
*/
|
|
28
|
|
|
protected $stream;
|
|
29
|
|
|
|
|
30
|
|
|
/**
|
|
31
|
|
|
* @var MessageProcessor
|
|
32
|
|
|
*/
|
|
33
|
|
|
protected $messageProcessor;
|
|
34
|
|
|
|
|
35
|
|
|
/**
|
|
36
|
|
|
* @var HandshakeInterface
|
|
37
|
|
|
*/
|
|
38
|
|
|
protected $handshake;
|
|
39
|
|
|
|
|
40
|
|
|
/**
|
|
41
|
|
|
* @var bool
|
|
42
|
|
|
*/
|
|
43
|
|
|
protected $handshakeDone;
|
|
44
|
|
|
|
|
45
|
|
|
/**
|
|
46
|
|
|
* @var string
|
|
47
|
|
|
*/
|
|
48
|
|
|
protected $uri;
|
|
49
|
|
|
|
|
50
|
|
|
/**
|
|
51
|
|
|
* @var MessageHandlerInterface|\Closure
|
|
52
|
|
|
*/
|
|
53
|
|
|
protected $handler;
|
|
54
|
|
|
|
|
55
|
5 |
|
public function __construct(MessageProcessor $messageProcessor, HandshakeInterface $handshake = null)
|
|
56
|
|
|
{
|
|
57
|
5 |
|
$this->handshake = $handshake;
|
|
58
|
5 |
|
$this->messageProcessor = $messageProcessor;
|
|
59
|
5 |
|
}
|
|
60
|
|
|
|
|
61
|
|
|
protected function onMessage(string $data)
|
|
62
|
|
|
{
|
|
63
|
|
|
try {
|
|
64
|
|
|
if (!$this->handshakeDone) {
|
|
65
|
|
|
$this->processHandshake($data);
|
|
66
|
|
|
} else {
|
|
67
|
|
|
$this->processMessage($data);
|
|
68
|
|
|
}
|
|
69
|
|
|
|
|
70
|
|
|
return;
|
|
71
|
|
|
} catch (WebsocketException $e) {
|
|
72
|
|
|
$this->messageProcessor->close($this->stream);
|
|
73
|
|
|
$this->logger->notice('Connection to ' . $this->getIp() . ' closed with error : ' . $e->getMessage());
|
|
74
|
|
|
$this->handler->onError($e, $this);
|
|
|
|
|
|
|
75
|
|
|
}
|
|
76
|
|
|
}
|
|
77
|
|
|
|
|
78
|
|
|
protected abstract function processHandshake(string $data);
|
|
|
|
|
|
|
79
|
|
|
protected abstract function processMessage(string $data);
|
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/**
|
|
82
|
|
|
* @return string
|
|
83
|
|
|
*/
|
|
84
|
|
|
public function getIp()
|
|
85
|
|
|
{
|
|
86
|
|
|
return $this->stream->getRemoteAddress();
|
|
87
|
|
|
}
|
|
88
|
|
|
|
|
89
|
|
|
/**
|
|
90
|
|
|
* @return \Psr\Log\LoggerInterface
|
|
91
|
|
|
*/
|
|
92
|
1 |
|
public function getLogger()
|
|
93
|
|
|
{
|
|
94
|
1 |
|
return $this->logger;
|
|
95
|
|
|
}
|
|
96
|
|
|
}
|
|
97
|
|
|
|
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: