1 | <?php |
||
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) |
||
91 | |||
92 | /** |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | function getSenderData() |
||
103 | |||
104 | function isOnline($user_id) |
||
105 | { |
||
106 | if(array_key_exists($user_id,$this->userAuthSocketMapper)) |
||
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.