-
Notifications
You must be signed in to change notification settings - Fork 0
/
svr.php
73 lines (59 loc) · 2.37 KB
/
svr.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
<?php
ini_set("log_errors","1");
ini_set("error_log",'../socket.log');
define('HOST_NAME',"0.0.0.0");
define('PORT',"1111");
$null = NULL;
require_once("zSocket.php");
$zsocket = new zSocket();
$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socketResource, 0, PORT);
socket_listen($socketResource);
$clientSocketArray = array($socketResource);
while (true) {
$newSocketArray = $clientSocketArray;
socket_select($newSocketArray, $null, $null, 0, 10);
if (in_array($socketResource, $newSocketArray)) {
echo 'connected';
$newSocket = socket_accept($socketResource);
$clientSocketArray[] = $newSocket;
$header = socket_read($newSocket, 1024);
$zsocket->doHandshake($header, $newSocket, HOST_NAME, PORT);
socket_getpeername($newSocket, $client_ip_address);
$connectionACK = $zsocket->newConnectionACK($client_ip_address);
$zsocket->send($connectionACK);
$newSocketIndex = array_search($socketResource, $newSocketArray);
unset($newSocketArray[$newSocketIndex]);
}
foreach ($newSocketArray as $newSocketArrayResource) {
while(socket_recv($newSocketArrayResource, $socketData, 1024, 0) >= 1){
$socketMessage = $zsocket->unseal($socketData);
$decodeData = base64_encode($socketData);
echo "socketData\n";
echo "=>".$socketData;
echo "end socketData\n\n";
echo "socketMessage\n";
echo $socketMessage;
echo "end socketMessage\n";
$msg = [
"type"=>"send",
];
//$messageObj = json_decode($socketMessage);
//$chat_box_message = $chatHandler->createChatBoxMessage($messageObj->chat_user, $messageObj->chat_message);
$chat_box_message = $zsocket->createChatBoxMessage("zMessing", $socketMessage);
$zsocket->send($chat_box_message);
break 2;
}
$socketData = @socket_read($newSocketArrayResource, 1024, PHP_NORMAL_READ);
if ($socketData === false) {
socket_getpeername($newSocketArrayResource, $client_ip_address);
$connectionACK = $zsocket->connectionDisconnectACK($client_ip_address);
$zsocket->send($connectionACK);
$newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
unset($clientSocketArray[$newSocketIndex]);
}
}
}
socket_close($socketResource);
?>