|
1
|
|
|
<?php |
|
2
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Created by PhpStorm. |
|
6
|
|
|
* User: shanmaseen |
|
7
|
|
|
* Date: 23/03/19 |
|
8
|
|
|
* Time: 07:40 م |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Shamaseen\Laravel\Ratchet; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Validation\ValidationException; |
|
14
|
|
|
use Shamaseen\Laravel\Ratchet\Exceptions\WebSocketException; |
|
15
|
|
|
use Shamaseen\Laravel\Ratchet\Facades\WsRoute; |
|
16
|
|
|
use Shamaseen\Laravel\Ratchet\Objects\Clients\Client; |
|
17
|
|
|
use Ratchet\ConnectionInterface; |
|
18
|
|
|
use Ratchet\MessageComponentInterface; |
|
19
|
|
|
use Shamaseen\Laravel\Ratchet\Traits\WebSocketMessagesManager; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class WebSocket |
|
23
|
|
|
* @package App\WebSockets |
|
24
|
|
|
*/ |
|
25
|
|
|
class Receiver implements MessageComponentInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use WebSocketMessagesManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Client[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public $clients; |
|
33
|
|
|
private $routes; |
|
34
|
|
|
public $userAuthSocketMapper; |
|
35
|
|
|
public $rooms = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* WebSocket constructor. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->clients = []; |
|
43
|
|
|
/** |
|
44
|
|
|
* The key will be auth id, the value will be resourceId |
|
45
|
|
|
*/ |
|
46
|
|
|
$this->userAuthSocketMapper = []; |
|
47
|
|
|
|
|
48
|
|
|
$this->mainRoutes(); |
|
49
|
|
|
include base_path().'/routes/websocket.php'; |
|
50
|
|
|
$this->routes = WsRoute::getRoutes(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ConnectionInterface $conn |
|
55
|
|
|
*/ |
|
56
|
|
|
public function onOpen(ConnectionInterface $conn) { |
|
57
|
|
|
|
|
58
|
|
|
$this->clients[$conn->resourceId] = new Client(); |
|
|
|
|
|
|
59
|
|
|
$this->clients[$conn->resourceId]->conn = $conn; |
|
|
|
|
|
|
60
|
|
|
// $this->clients[$conn->resourceId]->resourceId = $conn->resourceId; |
|
61
|
|
|
|
|
62
|
|
|
echo "New connection! ({$conn->resourceId})\n"; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ConnectionInterface $from |
|
67
|
|
|
* @param string $msg |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
*/ |
|
70
|
|
|
public function onMessage(ConnectionInterface $from, $msg) { |
|
71
|
|
|
try |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$msg = json_decode($msg); |
|
74
|
|
|
|
|
75
|
|
|
$this->checkForRequiredInMessage($msg,$from); |
|
76
|
|
|
|
|
77
|
|
|
\Session::setId($msg->session); |
|
78
|
|
|
|
|
79
|
|
|
\Session::start(); |
|
80
|
|
|
|
|
81
|
|
|
$route = $this->routes[$msg->route]; |
|
82
|
|
|
if($route->auth && !\Auth::check()) |
|
|
|
|
|
|
83
|
|
|
$this->error($msg,$from,'Unauthenticated.'); |
|
84
|
|
|
else |
|
|
|
|
|
|
85
|
|
|
$this->userAuthSocketMapper[\Auth::id()] = $from->resourceId; |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$class = $route->controller; |
|
88
|
|
|
$method = $route->method; |
|
89
|
|
|
$controller = new $class; |
|
90
|
|
|
|
|
91
|
|
|
$this->cloneProperties($this,$controller); |
|
92
|
|
|
|
|
93
|
|
|
$controller->conn = $from; |
|
94
|
|
|
$controller->receiver = $this; |
|
95
|
|
|
$controller->request = $msg; |
|
96
|
|
|
$controller->route = $route; |
|
97
|
|
|
|
|
98
|
|
|
if(!method_exists($controller,$method)) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
$this->error($msg,$from,'Method doesnt\'t exist !'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$controller->$method(); |
|
104
|
|
|
} |
|
105
|
|
|
catch (WebSocketException $exception) |
|
|
|
|
|
|
106
|
|
|
{ |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
catch(ValidationException $exception) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$this->sendToWebSocketUser($from,[ |
|
112
|
|
|
'message'=>$exception->getMessage(), |
|
113
|
|
|
'errors'=> $exception->errors() |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param ConnectionInterface $conn |
|
120
|
|
|
*/ |
|
121
|
|
|
public function onClose(ConnectionInterface $conn) { |
|
122
|
|
|
// The connection is closed, remove it, as we can no longer send it messages |
|
123
|
|
|
unset($this->clients[$conn->resourceId]); |
|
124
|
|
|
|
|
125
|
|
|
echo "Connection {$conn->resourceId} has disconnected\n"; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param ConnectionInterface $conn |
|
130
|
|
|
* @param \Exception $e |
|
131
|
|
|
*/ |
|
132
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) { |
|
|
|
|
|
|
133
|
|
|
echo "An error has occurred: {$e->getMessage()}\n"; |
|
134
|
|
|
echo "In {$e->getFile()} line {$e->getLine()}\n"; |
|
135
|
|
|
|
|
136
|
|
|
$conn->close(); |
|
137
|
|
|
echo 'end'; |
|
138
|
|
|
die; |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param $msg |
|
143
|
|
|
* @param $from |
|
144
|
|
|
* @throws WebSocketException |
|
145
|
|
|
*/ |
|
146
|
|
|
function checkForRequiredInMessage($msg,$from) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
if(!isset($msg->route) || !isset($msg->session)) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
$this->error($msg,$from,'You can\'t send a request without the route and the session id !'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if(!isset($this->routes[$msg->route])) |
|
|
|
|
|
|
154
|
|
|
$this->error($msg,$from,'No such route !'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param $clonedObject |
|
159
|
|
|
* @param $clone |
|
160
|
|
|
*/ |
|
161
|
|
|
function cloneProperties($clonedObject, $clone) |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
foreach (get_object_vars($clonedObject) as $key => $value) { |
|
164
|
|
|
$clone->$key = $value; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
function mainRoutes() |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
|
|
WsRoute::make('initializeWebsocket','Shamaseen\Laravel\Ratchet\Controllers\InitializeController','index'); |
|
171
|
|
|
WsRoute::make('room-enter','Shamaseen\Laravel\Ratchet\Controllers\RoomController','enterRoom'); |
|
172
|
|
|
WsRoute::make('room-exit','Shamaseen\Laravel\Ratchet\Controllers\RoomController','exitRoom'); |
|
173
|
|
|
WsRoute::make('send-to-user','Shamaseen\Laravel\Ratchet\Controllers\ChatController','sendMessageToUser'); |
|
174
|
|
|
WsRoute::make('send-to-room','Shamaseen\Laravel\Ratchet\Controllers\ChatController','sendMessageToRoom'); |
|
175
|
|
|
} |
|
176
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: