forked from ghedipunk/PHP-Websockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myServidor.php
executable file
·167 lines (153 loc) · 4.59 KB
/
myServidor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
include_once 'websocketserver.php';
class myServidor extends WebSocketServer
{
private $usuarios = array();
private $eventos = array();
protected function process($user, $message)
{
$deco = json_decode($message);
if ($deco->tipo == 'on') {
$this->agregar_evento($deco->evento, $user);
}
if ($deco->tipo == 'emit') {
$this->avisar_evento($deco->evento, $message);
}
if ($deco->tipo == 'mensaje') {
if ($deco->para == '0') {
foreach ($this->users as $key => $usuario) {
$this->send($usuario, $message);
}
} else {
$this->send($this->usuario_by_id($deco->para), $message);
}
}
if ($deco->tipo == 'registro') {
foreach ($this->usuarios as $key => $value) {
if ($value['id'] == $user->id) {
$this->usuarios[$key]['usuario'] = $deco->datos;
break;
}
}
$this->listar_usuarios();
$this->enviar_usuarios();
$this->enviar_identificador($user);
}
if ($deco->tipo == 'usuarios') {
$this->enviar_usuarios();
}
}
// hay una conexión
protected function connected($user)
{
array_push($this->usuarios, array('id' => $user->id, 'usuario' => $user->id));
$this->listar_usuarios();
$this->enviar_usuarios();
$this->enviar_identificador($user);
}
// hay una desconexión
protected function closed($user)
{
$this->listar_usuarios();
$this->enviar_usuarios();
}
public function agregar_evento($evento, $user)
{
if (array_key_exists($evento, $this->eventos)) {
array_push($this->eventos[$evento], $user->id);
} else {
$this->eventos[$evento] = array($user->id);
}
}
public function avisar_evento($evento, $msg)
{
$destino = array();
if (array_key_exists($evento, $this->eventos)) {
$destino = $this->eventos[$evento];
}
if (count($destino) > 0) {
foreach ($destino as $key => $id) {
if ($this->existe_id($id)) {
$this->send($this->usuario_by_id($id), $msg);
}
}
}
}
public function limpiar_usuarios()
{
$e = array();
$nuevo = array();
foreach ($this->users as $k => $user) {
foreach ($this->usuarios as $key => $usuario) {
if ($user->id == $usuario['id']) {
array_push($nuevo, $usuario);
}
}
}
$this->usuarios = $nuevo;
}
private function enviar_identificador($user)
{
$u = array();
foreach ($this->usuarios as $key => $usuario) {
if ($user->id == $usuario['id']) {
$u = $usuario;
}
}
$a = array('tipo' => 'identificador', 'datos' => $u);
$b = json_encode($a);
$this->send($user, $b);
}
private function enviar_usuarios()
{
if (count($this->usuarios) > 0) {
$a = array('tipo' => 'usuarios', 'datos' => $this->usuarios);
$b = json_encode($a);
foreach ($this->users as $key => $usuario) {
$this->send($usuario, $b);
}
}
}
private function usuario_by_id($id = '')
{
$u = $id;
foreach ($this->users as $key => $user) {
if ($user->id == $id) {
$u = $user;
}
}
return $u;
}
private function existe_id($id = '')
{
$f = false;
foreach ($this->users as $key => $user) {
if ($user->id == $id) {
$f = true;
break;
}
}
return $f;
}
private function listar_usuarios($value = '')
{
$this->limpiar_usuarios();
if (count($this->usuarios) > 0) {
echo '-------- Usuario/s conectado/s ('.count($this->usuarios).") --------\n";
$i = 0;
foreach ($this->usuarios as $key => $value) {
$i++;
echo '---- '.$i.'- '.$value['id'].': '.$value['usuario']."\n";
}
echo "-------------------------------------------\n";
} else {
echo "-------- No hay usuarios conectados --------\n";
}
}
}
$echo = new myServidor('127.0.0.1', '8080');
try {
$echo->run();
} catch (Exception $e) {
$echo->stdout($e->getMessage());
}