diff --git a/build/kwebsocket/core/wsinterop.php b/build/kwebsocket/core/wsinterop.php new file mode 100644 index 0000000..8867421 --- /dev/null +++ b/build/kwebsocket/core/wsinterop.php @@ -0,0 +1,54 @@ +socket = socket_create(AF_INET, SOCK_STREAM, 0); + $this->isOpened = socket_connect($this->socket,$host,$port); + $this->host = $host; + $this->port = $port; + + socket_set_block($this->socket); + + } + + public function send(array $data) : bool{ + + if( !$this->isOpened ) return false; + + $toSend = json_encode($data); + $size = strlen($toSend); + + socket_write($this->socket,pack('N',$size)); + $success = socket_write($this->socket,$toSend); + + return $success == $size; + } + + public function receive() : array{ + $size = unpack("N",socket_read($this->socket,4))[1]; + $read = 0; + $data = ""; + $tmp = ""; + + while($read != $size){ + + $remaining = ($size - $read); + $read += socket_recv($this->socket,$tmp,$remaining,MSG_DONTWAIT); + $data .= $tmp; + + } + + return json_decode($data,true); + } + + } \ No newline at end of file diff --git a/public_html/index.php b/public_html/index.php index f1270f9..12ea6cf 100755 --- a/public_html/index.php +++ b/public_html/index.php @@ -5,15 +5,46 @@ use \api\core\Request; use \api\core\AuthSystemDefault; use \database\core\DatabaseDriver; + use \kwebsocket\core\wsinterop; /* (1) Start session */ session_start(); - if( count($_SESSION['USER']) > 0 ) - $_SESSION['NAME'] = $_SESSION['USER']['username']; - elseif( count($_SESSION['ADMIN']) > 0 ) - $_SESSION['NAME'] = $_SESSION['ADMIN']['username']; - elseif( !isset($_SESSION['NAME']) || strlen($_SESSION['NAME']) == 0 ) - $_SESSION['NAME'] = 'guest'.uniqid(); + if( !isset($_SESSION['NAME']) || strlen($_SESSION['NAME']) == 0 ){ + + // ask with websocketInterop + $wsi = new wsinterop('localhost', 9998); + + if( count($_SESSION['USER']) > 0 ){ + + // get/send name to web socket + $wsi->send(['type' => 'user', 'name' => $_SESSION['USER']['username']]); + $check = $wsi->receive(); + + if( $check['error'] == false ) + $_SESSION['NAME'] = $check['name']; + + }elseif( count($_SESSION['ADMIN']) > 0 ){ + + // get/send name to web socket + $wsi->send(['type' => 'admin', 'name' => $_SESSION['ADMIN']['username']]); + $check = $wsi->receive(); + + if( $check['error'] == false ) + $_SESSION['NAME'] = $check['name']; + + }else{ + + // get/send name to web socket + $wsi->send(['type' => 'guest', 'name' => null]); + $check = $wsi->receive(); + + if( $check['error'] == false ) + $_SESSION['NAME'] = $check['name']; + } + + $wsi = null; + + } /* (2) Set default Driver for Repos */ Repo::setDriver(DatabaseDriver::get());