-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreasocket.php
More file actions
107 lines (99 loc) · 3.76 KB
/
creasocket.php
File metadata and controls
107 lines (99 loc) · 3.76 KB
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
#!/usr/bin/env php
<?php
require_once('./websockets.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) {
$this->send($user,$message);
}
protected function connected ($user) {
// Do nothing: This is just an echo server, there's no need to track the user.
// However, if we did care about the users, we would probably have a cookie to
// parse at this step, would be looking them up in permanent storage, etc.
}
protected function closed ($user) {
// Do nothing: This is where cleanup would go, in case the user had any sort of
// open files or other objects associated with them. This runs after the socket
// has been closed, so there is no need to clean up the socket itself here.
$user->disconnect();
}
//Errors list
//001 invalid token
//002 invalid command
//003 invalid module
//004 missing parameters
//005
//006
//007
protected function send($user,$message) {
if (!$user->authenticated){
$command = explode("|",$message);
if (count($command)>1 && $command[0] == 'CRAUTH'){
if ($user->authenticate($command[1])){
$message = $this->frame('ACK',$user);
$result = @socket_write($user->socket, $message, strlen($message));
//$message = $this->frame('USR:'.$user->username,$user);
//$result = @socket_write($user->socket, $message, strlen($message));
}else{
$message = $this->frame('ERR-001',$user);
$result = @socket_write($user->socket, $message, strlen($message));
}
}else{
$message = $this->frame('ERR-002',$user);
$result = @socket_write($user->socket, $message, strlen($message));
}
}else{
if (!$user->subscribed){
$command = explode("|",$message);
if (count($command)>1 && $command[0] == 'SUBSCR'){
if ($user->subscribe($command[1])){
$message = $this->frame('ACK',$user);
$result = @socket_write($user->socket, $message, strlen($message));
}else{
$message = $this->frame('ERR-003',$user);
$result = @socket_write($user->socket, $message, strlen($message));
}
}else{
$message = $this->frame('ERR-002',$user);
$result = @socket_write($user->socket, $message, strlen($message));
}
}else{
$command = explode("|",$message);
if (count($command)>0){
switch ($command[0]) {
case 'GET':
$message = $user->process();
$message = $this->frame($message,$user);
$result = @socket_write($user->socket, $message, strlen($message));
break;
case 'SEND':
if (count($command)>2){
$message = $user->send($command[1], $command[2]);
$message = $this->frame($message,$user);
$result = @socket_write($user->socket, $message, strlen($message));
}else{
$message = $this->frame('ERR-004',$user);
$result = @socket_write($user->socket, $message, strlen($message));
}
break;
default:
//$this->stdout("> $message");
$message = $this->frame('ERR-002',$user);
$result = @socket_write($user->socket, $message, strlen($message));
break;
}
}else{
$message = $this->frame('ERR-002',$user);
$result = @socket_write($user->socket, $message, strlen($message));
}
}
}
}
}
$echo = new echoServer("0.0.0.0","9000");
try {
$echo->run();
}
catch (Exception $e) {
$echo->stdout($e->getMessage());
}