-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.php
52 lines (42 loc) · 1.03 KB
/
server.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
#!/usr/bin/env php
<?php
require_once('chat.php');
class echoServer extends WebSocketServer
{
//protected $maxBufferSize = 1048576; //1MB... overkill for an echo server, but potentially plausible for other applications.
protected function process ($user, $message)
{
$message = array(
'nome' => $user->nome,
'msg' => htmlentities($message)
);
foreach ($this->users as $currentUser) {
$this->send($currentUser, json_encode($message));
}
}
protected function connected ($user)
{
$header = $this->parse_get_header($user->requestedResource);
$usuario = get_usuario_by_id($header['token']);
if ( ! $usuario )
{
return $this->disconnect($user->socket);
}
$this->users[$user->id]->nome = $usuario['nick'];
$this->users[$user->id]->token = $header['token'];
update_usuario_to_online($header['token']);
}
protected function closed ($user)
{
update_usuario_by_id($user->token);
}
}
$echo = new echoServer("0.0.0.0","8888");
try
{
$echo->run();
}
catch (Exception $e)
{
$echo->stdout($e->getMessage());
}