-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_server.php
157 lines (127 loc) · 4.02 KB
/
websocket_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
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
<?php
// Run from command prompt > php asdasd.php
require_once("db.php");
chdir('phpws');
require_once("websocket.server.php");
/**
clase q maneja los mensajes enviados a /vr/
repite todo lo q le llega a todos los usuarios conectados al mismo juego
ademas guarda en bd las acciones
*/
class VRHandler extends WebSocketUriHandler{
public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg){
try{
//safari termina sus mensajes con un 0xFF...
$data_txt = $msg->getData();
$data_txt = preg_replace('/^[^{]*(\{.+\})[^}]+$/', '$1', $data_txt);
if(empty($data_txt)) return;
$data = json_decode($data_txt);
if(isset($data->apagar)) exit;
$amigos = $this->amigos($user);
if(empty($amigos)) return;
foreach($amigos as $u) $u->sendMessage($msg);
$id = $this->param($user, 'id');
$db = new DB();
switch($data->accion){
case 'mover':
$update = array();
if(isset($data->vueltas)) $update['vueltas'] = $data->vueltas;
if(isset($data->lugar)) $update['lugar'] = $data->lugar;
if(isset($data->choque)){
$update['choque_x'] = $data->choque ? $data->choque->x : null;
$update['choque_y'] = $data->choque ? $data->choque->y : null;
}
if(!empty($update)) $db->update('jugadores', $update, array('id' => $id));
case 'posicion':
$db->insert('movimientos', array(
'jugador_id' => $id,
'pos_x' => $data->pos->x,
'pos_y' => $data->pos->y));
break;
case 'join':
$online = array();
foreach($amigos as $u){
if($this->param($u, 'id')) $online[] = $this->param($u, 'id');
}
$msg->setData(json_encode(array(
'accion' => 'lista_online',
'ids' => $online)));
$user->sendMessage($msg);
break;
case 'chat':
$db->insert('chats', array(
'jugador_id' => $id,
'juego_id' => $this->param($user, 'juego'),
'txt' => $data->msg
));
}
}
catch(Exception $e){
$this->say('FAIL: '.$e->getMessage());
}
}
public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $obj){
$this->say("[VR] Admin TEST received!");
$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
}
function param(IWebSocketConnection $user, $par){
if(!isset($user->parameters[$par])) return null;
return $user->parameters[$par];
}
function amigos(IWebSocketConnection $user){
$juego = $this->param($user, 'juego');
$users = array();
foreach($this->users as $u){
if($this->param($u, 'juego') == $juego) $users[] = $u;
}
return $users;
}
function despedir(IWebSocketConnection $user){
//$msg = new WebSocketMessage();
$amigos = $this->amigos($user);
//foreach($amigos as $u) $u->sendMessage($msg);
}
}
/**
* VR socket server. Implements the basic eventlisteners and attaches a resource handler for /echo/ urls.
*
*
* @author Chris
*
*/
class VRSocketServer implements IWebSocketServerObserver{
protected $debug = false;
protected $server;
var $handler = null;
public function __construct(){
$this->server = new WebSocketServer(0, 12345, 'superdupersecretkey23');
$this->server->addObserver($this);
$this->handler = new VRHandler();
$this->server->addUriHandler("vr", $this->handler);
}
public function onConnect(IWebSocketConnection $user){
$this->say("[VR] {$user->getId()} connected: ".print_r($user->parameters, true));
}
public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg){
$this->say("[VR] {$user->getId()} says '{$msg->getData()}'");
}
public function onDisconnect(IWebSocketConnection $user){
$this->say("[VR] {$user->getId()} disconnected");
$this->handler->despedir($user);
}
public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $msg){
$this->say("[VR] Admin Message received!");
$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
}
public function say($msg){
echo "$msg \r\n";
}
public function run(){
$this->server->run();
}
}
// Start server
$server = new VRSocketServer(0,12345);
$server->run();