99 lines
2.5 KiB
PHP
99 lines
2.5 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ERROR);
|
|
/* Allow the script to hang around waiting for connections. */
|
|
set_time_limit(0);
|
|
|
|
/* Turn on implicit output flushing so we see what we're getting
|
|
* as it comes in. */
|
|
//ob_implicit_flush();
|
|
|
|
//$address = 'mojo.bateworld.com';
|
|
$address = '99.192.174.19';
|
|
$port = 8080;
|
|
|
|
$cert = '/etc/letsencrypt/live/mojo.bateworld.com-0001/cert.pem';
|
|
// Create WebSocket.
|
|
//$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
|
|
|
$port = 8080;
|
|
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('ERROR 1');
|
|
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1) or die('ERROR 1');
|
|
socket_bind($sock, $address, $port) or die('ERROR 1');
|
|
socket_listen($sock) or die('ERROR 1');
|
|
|
|
$request = socket_read($sock, 100);
|
|
|
|
socket_write($sock, $headers, strlen($headers));
|
|
|
|
$clients = array($sock);
|
|
//$client2 = socket_accept($sock);
|
|
while (true) {
|
|
|
|
|
|
|
|
$read = $clients;
|
|
if (socket_select($read, $write = null, $except = null, 0) < 1){
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket_write($read, "hello");
|
|
if (in_array($sock, $read)) {echo "WHY";
|
|
$clients[] = $newsock = socket_accept($sock);
|
|
$key = array_search($sock, $read);
|
|
unset($read[$key]);
|
|
$cid = array_search($newsock, $clients);
|
|
$tracker[$cid]['login'] = false;
|
|
}
|
|
foreach ($read as $read_sock) {
|
|
|
|
/*
|
|
$buf = socket_read($read_sock, 2048, PHP_NORMAL_READ);
|
|
|
|
|
|
if(preg_match("/Sec-WebSocket-Key: (.*)/",$buf,$match)){
|
|
|
|
$key = base64_encode(pack(
|
|
'H*',
|
|
//sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
|
|
hash("sha1",trim($match[1])."258EAFA5-E914-47DA-95CA-C5AB0DC85B11",true)
|
|
));
|
|
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
|
|
$headers .= "Upgrade: websocket\r\n";
|
|
$headers .= "Connection: Upgrade\r\n";
|
|
$headers .= "Sec-WebSocket-Version: 13\r\n";
|
|
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
|
|
|
|
|
|
//SERVER RESPONSE ----
|
|
socket_write($sock,$headers,strlen($headers));
|
|
|
|
echo "handshake done...";
|
|
}else{echo "bad handshake"; exit;}*/
|
|
|
|
$cid = array_search($read_sock, $clients);
|
|
$data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
|
|
if ($data === false) {
|
|
unset($clients[$cid]);
|
|
continue;
|
|
}
|
|
$data = trim($data);
|
|
if (!empty($data)) {echo "what";
|
|
echo $data." \n \n hello \n";
|
|
$data=str_split($data);
|
|
foreach($data as $value){
|
|
echo ord($value)." ";
|
|
socket_write($read_sock, "hello");
|
|
}
|
|
socket_write($read_sock, "hello");
|
|
echo "\n-----------------------------------------------------------------------\n";
|
|
}
|
|
}
|
|
}
|
|
?>
|