1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-06 03:50:04 +02:00

Update socket

This commit is contained in:
Daniel Neto 2024-10-07 12:23:54 -03:00
parent 251e5740f0
commit a2380e37bd
4 changed files with 51 additions and 19 deletions

View file

@ -415,6 +415,7 @@ class Message implements MessageComponentInterface {
public function onError(ConnectionInterface $conn, \Exception $e) {
dbDeleteConnection($conn->resourceId);
_error_log("resourceId={$conn->resourceId} close on line ".__LINE__);
$conn->close();
}
@ -500,6 +501,7 @@ class Message implements MessageComponentInterface {
if($client['time']+$this->disconnectAfter < $time){
//_error_log("resourceId={$client['resourceId']} is too old, close it");
if(!empty($this->clients[$client['resourceId']])){
_error_log("resourceId={$client['resourceId']} close on line ".__LINE__);
$this->clients[$client['resourceId']]->close();
}
unset($this->clients[$client['resourceId']]);

View file

@ -47,7 +47,7 @@ class YPTSocket extends PluginAbstract
public static function getServerVersion()
{
return "7.0";
return "7.1";
}
public function updateScript()

View file

@ -165,6 +165,7 @@ function socketConnect() {
onSocketClose();
};
function checkNetworkConnection() {
if (!navigator.onLine) {
console.error('It seems you are offline. Check your internet connection.');
@ -195,7 +196,7 @@ function socketConnect() {
conn.onerror = function (err) {
socketConnectRequested = 0;
console.error('Socket encountered error: ', err.message, 'URL:', url);
console.error('Socket encountered error: ', err, 'URL:', url);
if (err.target.readyState === WebSocket.CLOSED) {
console.error('WebSocket is in CLOSED state. Likely a network or server issue.');
} else if (err.target.readyState === WebSocket.CLOSING) {

View file

@ -163,38 +163,67 @@ if ((strtolower($scheme) !== 'https' || !empty($SocketDataObj->forceNonSecure))
foreach ($parameters as $key => $value) {
echo "Parameter [{$key}]: $value " . PHP_EOL;
}
echo "Starting ... {$SocketDataObj->uri}:{$SocketDataObj->port}" . PHP_EOL;
echo "DO NOT CLOSE THIS TERMINAL " . PHP_EOL;
// Create WebSocket Server
$webSock = new ReactServer($SocketDataObj->uri . ':' . $SocketDataObj->port, $loop);
$webSock = new SecureServer($webSock, $loop, $parameters);
// Handle HTTP requests using guzzlehttp/psr7
// Handle HTTP requests using guzzlehttp/psr7 and add CORS headers
$httpServer = function (ServerRequestInterface $request) {
return new Response(
200,
['Content-Type' => 'text/plain'],
[
'Content-Type' => 'text/plain',
'Access-Control-Allow-Origin' => '*', // Allow all origins
'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS', // Allow all necessary methods
'Access-Control-Allow-Headers' => 'Content-Type, Authorization', // Allow any headers needed
'Access-Control-Allow-Credentials' => 'true', // Allow credentials if necessary
],
"Socket server is running. SSL is valid."
);
};
// Handle incoming connections and handle HTTP requests
// Handle incoming connections and differentiate between WebSocket and HTTP requests
$webSock->on('connection', function ($conn) use ($httpServer) {
$conn->on('data', function ($data) use ($conn, $httpServer) {
// Simulate an HTTP request and return a response indicating the server status
// Parse headers from the incoming connection
$headers = [];
foreach (explode("\r\n", $data) as $line) {
if (strpos($line, ':') !== false) {
list($key, $value) = explode(':', $line, 2);
$headers[trim($key)] = trim($value);
}
}
// Check if the Upgrade header is present and indicates a WebSocket connection
if (isset($headers['Upgrade']) && strtolower($headers['Upgrade']) === 'websocket') {
// It's a WebSocket connection; do nothing here, as WebSocket connections are handled elsewhere
return;
} else {
// It's an HTTP request, simulate an HTTP request and return a response
$response = $httpServer(new ServerRequest('GET', '/'));
// Send an HTTP-compliant response back to the client
$headers = "HTTP/1.1 200 OK\r\n";
$headers .= "Content-Type: text/plain\r\n";
$headers .= "Content-Length: " . strlen((string) $response->getBody()) . "\r\n";
$headers .= "Connection: close\r\n\r\n";
// Send an HTTP-compliant response back to the client, with CORS headers included
$httpHeaders = "HTTP/1.1 200 OK\r\n";
$httpHeaders .= "Content-Type: text/plain\r\n";
$httpHeaders .= "Access-Control-Allow-Origin: *\r\n"; // Allow all origins
$httpHeaders .= "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n"; // Allow all methods
$httpHeaders .= "Access-Control-Allow-Headers: Content-Type, Authorization\r\n"; // Allow necessary headers
$httpHeaders .= "Access-Control-Allow-Credentials: true\r\n"; // Allow credentials if needed
$httpHeaders .= "Content-Length: " . strlen((string) $response->getBody()) . "\r\n";
$httpHeaders .= "Connection: close\r\n\r\n";
$conn->write($headers . $response->getBody());
$conn->write($httpHeaders . $response->getBody());
$conn->end();
}
});
});
// Create WebSocket server instance
$webServer = new IoServer(
new HttpServer(