1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: shanmaseen |
5
|
|
|
* Date: 23/03/19 |
6
|
|
|
* Time: 10:15 م |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Shamaseen\Laravel\Ratchet\Traits; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Shamaseen\Laravel\Ratchet\Exceptions\WebSocketException; |
13
|
|
|
use Ratchet\ConnectionInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait WebSocketMessagesManager |
17
|
|
|
* @package App\WebSockets |
18
|
|
|
*/ |
19
|
|
|
trait WebSocketMessagesManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param $request |
23
|
|
|
* @param ConnectionInterface $from |
24
|
|
|
* @param $error |
25
|
|
|
* @throws WebSocketException |
26
|
|
|
*/ |
27
|
|
|
function error($request,ConnectionInterface $from, $error) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
if(env('APP_DEBUG')) |
30
|
|
|
{ |
31
|
|
|
echo 'User error: '; |
32
|
|
|
echo $error."\n"; |
33
|
|
|
print_r($request); |
34
|
|
|
echo " ============================================================== \n \n \n"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$data = [ |
38
|
|
|
'event'=>'error', |
39
|
|
|
'message'=>$error |
40
|
|
|
]; |
41
|
|
|
$this->sendToWebSocketUser($from,$data); |
42
|
|
|
throw new WebSocketException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ConnectionInterface $conn |
47
|
|
|
* @param $data |
48
|
|
|
*/ |
49
|
|
|
function sendToWebSocketUser(ConnectionInterface $conn,$data) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
if(!is_array($data)) |
52
|
|
|
$data = ['msg'=>$data,'event'=>'default']; |
53
|
|
|
|
54
|
|
|
if(!isset($data['event'])) |
55
|
|
|
{ |
56
|
|
|
$data['event'] = 'default'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if(isset($this->route) && !array_key_exists('sender',$data)) |
60
|
|
|
$data['sender'] = $this->getSenderData(); |
61
|
|
|
|
62
|
|
|
$conn->send(json_encode($data)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $user_id |
67
|
|
|
* @param array $data |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
function sendToUser($user_id,$data) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if(!$this->isOnline($user_id)) |
73
|
|
|
{ |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$resourceId = $this->userAuthSocketMapper[$user_id]; |
|
|
|
|
78
|
|
|
/** @var ConnectionInterface $conn */ |
79
|
|
|
$conn = $this->clients[$resourceId]->conn; |
|
|
|
|
80
|
|
|
$this->sendToWebSocketUser($conn,$data); |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $data |
86
|
|
|
*/ |
87
|
|
|
function sendBack($data) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->sendToWebSocketUser($this->conn,$data); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
function getSenderData() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'id' => \Auth::id(), |
100
|
|
|
'name'=> \Auth::user()->name |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
function isOnline($user_id) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if(array_key_exists($user_id,$this->userAuthSocketMapper)) |
107
|
|
|
{ |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.