|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the slince/spike package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Slince <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Spike\Server\Listener; |
|
13
|
|
|
|
|
14
|
|
|
use React\Socket\ConnectionInterface; |
|
15
|
|
|
use Slince\EventDispatcher\Event; |
|
16
|
|
|
use Slince\EventDispatcher\SubscriberInterface; |
|
17
|
|
|
use Spike\Common\Exception\InvalidArgumentException; |
|
18
|
|
|
use Spike\Common\Protocol\SpikeInterface; |
|
19
|
|
|
use Spike\Server\Event\Events; |
|
20
|
|
|
use Spike\Server\Event\FilterActionHandlerEvent; |
|
21
|
|
|
use Spike\Server\Handler; |
|
22
|
|
|
use Spike\Server\Server; |
|
23
|
|
|
|
|
24
|
|
|
class ServerListener implements SubscriberInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function getSubscribedEvents() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
Events::SERVER_ACTION => 'onServerAction', |
|
33
|
|
|
Events::CONNECTION_ERROR => 'onConnectionError', |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Event $event |
|
39
|
|
|
*/ |
|
40
|
|
|
public function onConnectionError(Event $event) |
|
41
|
|
|
{ |
|
42
|
|
|
$event->getArgument('connection')->end('bad message'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param FilterActionHandlerEvent $event |
|
47
|
|
|
*/ |
|
48
|
|
|
public function onServerAction(FilterActionHandlerEvent $event) |
|
49
|
|
|
{ |
|
50
|
|
|
$actionHandler = $this->createMessageHandler( |
|
51
|
|
|
$event->getSubject(), |
|
|
|
|
|
|
52
|
|
|
$event->getMessage(), |
|
53
|
|
|
$event->getConnection() |
|
54
|
|
|
); |
|
55
|
|
|
$event->setActionHandler($actionHandler); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Creates the handler for the received message. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Server $server |
|
62
|
|
|
* @param SpikeInterface $message |
|
63
|
|
|
* @param ConnectionInterface $connection |
|
64
|
|
|
* |
|
65
|
|
|
* @return Handler\ActionHandlerInterface |
|
66
|
|
|
* @codeCoverageIgnore |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function createMessageHandler(Server $server, SpikeInterface $message, ConnectionInterface $connection) |
|
69
|
|
|
{ |
|
70
|
|
|
switch ($message->getAction()) { |
|
71
|
|
|
case 'auth': |
|
72
|
|
|
$handler = new Handler\AuthHandler($server, $connection); |
|
73
|
|
|
break; |
|
74
|
|
|
case 'ping': |
|
75
|
|
|
$handler = new Handler\PingHandler($server, $connection); |
|
76
|
|
|
break; |
|
77
|
|
|
case 'register_tunnel': |
|
78
|
|
|
$handler = new Handler\RegisterTunnelHandler($server, $connection); |
|
79
|
|
|
break; |
|
80
|
|
|
case 'register_proxy': |
|
81
|
|
|
$handler = new Handler\RegisterProxyHandler($server, $connection); |
|
82
|
|
|
break; |
|
83
|
|
|
default: |
|
84
|
|
|
throw new InvalidArgumentException(sprintf('Cannot find handler for message type: "%s"', |
|
85
|
|
|
get_class($message) |
|
86
|
|
|
)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $handler; |
|
90
|
|
|
} |
|
91
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: