/
ChatServer.php
97 lines (83 loc) · 3.2 KB
/
ChatServer.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
#!/php -q
<?php
// Run from command prompt > php -q websocket.demo.php
// Basic WebSocket demo echoes msg back to client
include 'websockets.php';
include 'src/system/ImageProcessor.php';
class ChatServer extends WebSocketServer {
protected $CONNECTED_USR = array();
protected function process ($user, $message) {
echo "[INFO] : message received, ".$message;
$message = json_decode($message);
try {
if(!empty($message)) {
if($message->type == "init") {
$address = '';
socket_getpeername($user->socket, $address);
$user->user_info = $message->user_info;
$user->ip = $address;
$user->name = $message->name;
//연결된 유저 추가
array_push($this->CONNECTED_USR, $user);
} else {
$this->broadcast($message);
}
}
} catch(Exception $e) {
print '[ERROR] process error, '.$e;
}
}
protected function connected ($user) {
}
protected function broadcast($message) {
$IC = new ImageProcessor();
$response = array();
$response["type"] = $message->type;
$response["sender"] = $message->user_info;
//접속한 모든 유저에게 메시지를 보내는 함수
foreach($this->CONNECTED_USR as $node){
if($message->type == "msg") {
$flag = rand(0, 2);
switch($flag) {
case 0:
$image_data = $IC->text2Image($message->text, $node->user_info);
break;
case 1:
$image_data = $IC->text2Image($message->text, $node->ip);
break;
case 2:
$image_data = $IC->text2Image($message->text, $node->name);
break;
}
$response["img_type_white"] = base64_encode($image_data["white"]);
$response["img_type_yellow"] = base64_encode($image_data["yellow"]);
} else if ($message->type == "img") {
$flag = rand(0, 2);
$file_path = 'src/'.$message->file;
switch($flag) {
case 0:
$image_data = $IC->watermark($file_path, $node->user_info);
break;
case 1:
$image_data = $IC->watermark($file_path, $node->ip);
break;
case 2:
$image_data = $IC->watermark($file_path, $node->name);
break;
}
var_dump($response);
$response["img"] = base64_encode($image_data);
}
$this->send($node, json_encode($response));
}
}
protected function closed ($user) {
//todo : 연결이 끊긴 유저가 생기면 CONNECTED_USR 에서 삭제해야 한다
}
}
$chat_server = new ChatServer("0.0.0.0", "9000");
try {
$chat_server->run();
} catch (Exception $e) {
$chat_server->stdout($e->getMessage());
}