add: build.kwebsocket.core.wsinterop (added SeekDaSky websocket-interop lib) | upd: public.index ( added name gathering through websocket-interop)

This commit is contained in:
xdrm-brackets 2017-12-06 16:36:55 +01:00
parent 40967db18b
commit 1945158724
2 changed files with 91 additions and 6 deletions

View File

@ -0,0 +1,54 @@
<?php
namespace \kwebsocket\core;
class wsinterop{
private $socket;
private $host;
private $port;
private $isOpened;
public function __construct(String $host, int $port){
$this->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);
}
}

View File

@ -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());