mirror of
https://github.com/DanielnetoDotCom/YouPHPTube
synced 2025-10-06 03:50:04 +02:00
Update socket
This commit is contained in:
parent
251e5740f0
commit
a2380e37bd
4 changed files with 51 additions and 19 deletions
|
@ -415,6 +415,7 @@ class Message implements MessageComponentInterface {
|
||||||
|
|
||||||
public function onError(ConnectionInterface $conn, \Exception $e) {
|
public function onError(ConnectionInterface $conn, \Exception $e) {
|
||||||
dbDeleteConnection($conn->resourceId);
|
dbDeleteConnection($conn->resourceId);
|
||||||
|
_error_log("resourceId={$conn->resourceId} close on line ".__LINE__);
|
||||||
$conn->close();
|
$conn->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -500,6 +501,7 @@ class Message implements MessageComponentInterface {
|
||||||
if($client['time']+$this->disconnectAfter < $time){
|
if($client['time']+$this->disconnectAfter < $time){
|
||||||
//_error_log("resourceId={$client['resourceId']} is too old, close it");
|
//_error_log("resourceId={$client['resourceId']} is too old, close it");
|
||||||
if(!empty($this->clients[$client['resourceId']])){
|
if(!empty($this->clients[$client['resourceId']])){
|
||||||
|
_error_log("resourceId={$client['resourceId']} close on line ".__LINE__);
|
||||||
$this->clients[$client['resourceId']]->close();
|
$this->clients[$client['resourceId']]->close();
|
||||||
}
|
}
|
||||||
unset($this->clients[$client['resourceId']]);
|
unset($this->clients[$client['resourceId']]);
|
||||||
|
|
|
@ -47,7 +47,7 @@ class YPTSocket extends PluginAbstract
|
||||||
|
|
||||||
public static function getServerVersion()
|
public static function getServerVersion()
|
||||||
{
|
{
|
||||||
return "7.0";
|
return "7.1";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateScript()
|
public function updateScript()
|
||||||
|
|
|
@ -119,10 +119,10 @@ function socketConnect() {
|
||||||
|
|
||||||
conn.onclose = function (e) {
|
conn.onclose = function (e) {
|
||||||
socketConnectRequested = 0;
|
socketConnectRequested = 0;
|
||||||
|
|
||||||
if (e.code === 1006) {
|
if (e.code === 1006) {
|
||||||
console.error('WebSocket closed unexpectedly with code 1006. Investigating possible causes...');
|
console.error('WebSocket closed unexpectedly with code 1006. Investigating possible causes...');
|
||||||
|
|
||||||
// Check the WebSocket readyState to understand the closure phase
|
// Check the WebSocket readyState to understand the closure phase
|
||||||
switch (conn.readyState) {
|
switch (conn.readyState) {
|
||||||
case WebSocket.CONNECTING:
|
case WebSocket.CONNECTING:
|
||||||
|
@ -138,20 +138,20 @@ function socketConnect() {
|
||||||
console.error('WebSocket was already in CLOSED state.');
|
console.error('WebSocket was already in CLOSED state.');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.error('Likely causes for error code 1006:');
|
console.error('Likely causes for error code 1006:');
|
||||||
console.error('1. Network issues (e.g., connection interrupted or blocked by a firewall).');
|
console.error('1. Network issues (e.g., connection interrupted or blocked by a firewall).');
|
||||||
console.error('2. SSL/TLS handshake failure (for wss:// connections).');
|
console.error('2. SSL/TLS handshake failure (for wss:// connections).');
|
||||||
console.error('3. Server-side issues (e.g., server crashed, closed connection unexpectedly).');
|
console.error('3. Server-side issues (e.g., server crashed, closed connection unexpectedly).');
|
||||||
console.error('4. Incorrect WebSocket URL (e.g., invalid hostname or path).');
|
console.error('4. Incorrect WebSocket URL (e.g., invalid hostname or path).');
|
||||||
console.error('Retrying connection in ' + socketConnectRetryTimeout / 1000 + ' seconds.');
|
console.error('Retrying connection in ' + socketConnectRetryTimeout / 1000 + ' seconds.');
|
||||||
|
|
||||||
// Retry connection with exponential backoff
|
// Retry connection with exponential backoff
|
||||||
socketConnectTimeout = setTimeout(function () {
|
socketConnectTimeout = setTimeout(function () {
|
||||||
socketConnectRetryTimeout = Math.min(socketConnectRetryTimeout * 2, 60000); // Increase timeout up to 1 minute
|
socketConnectRetryTimeout = Math.min(socketConnectRetryTimeout * 2, 60000); // Increase timeout up to 1 minute
|
||||||
socketConnect();
|
socketConnect();
|
||||||
}, socketConnectRetryTimeout);
|
}, socketConnectRetryTimeout);
|
||||||
|
|
||||||
// Optionally, add checks for connection timeouts, SSL issues, or network connectivity
|
// Optionally, add checks for connection timeouts, SSL issues, or network connectivity
|
||||||
checkNetworkConnection();
|
checkNetworkConnection();
|
||||||
checkSSLIssues(webSocketURL);
|
checkSSLIssues(webSocketURL);
|
||||||
|
@ -161,9 +161,10 @@ function socketConnect() {
|
||||||
socketConnect();
|
socketConnect();
|
||||||
}, socketConnectRetryTimeout);
|
}, socketConnectRetryTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
onSocketClose();
|
onSocketClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function checkNetworkConnection() {
|
function checkNetworkConnection() {
|
||||||
if (!navigator.onLine) {
|
if (!navigator.onLine) {
|
||||||
|
@ -195,7 +196,7 @@ function socketConnect() {
|
||||||
|
|
||||||
conn.onerror = function (err) {
|
conn.onerror = function (err) {
|
||||||
socketConnectRequested = 0;
|
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) {
|
if (err.target.readyState === WebSocket.CLOSED) {
|
||||||
console.error('WebSocket is in CLOSED state. Likely a network or server issue.');
|
console.error('WebSocket is in CLOSED state. Likely a network or server issue.');
|
||||||
} else if (err.target.readyState === WebSocket.CLOSING) {
|
} else if (err.target.readyState === WebSocket.CLOSING) {
|
||||||
|
|
|
@ -163,38 +163,67 @@ if ((strtolower($scheme) !== 'https' || !empty($SocketDataObj->forceNonSecure))
|
||||||
foreach ($parameters as $key => $value) {
|
foreach ($parameters as $key => $value) {
|
||||||
echo "Parameter [{$key}]: $value " . PHP_EOL;
|
echo "Parameter [{$key}]: $value " . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
echo "Starting ... {$SocketDataObj->uri}:{$SocketDataObj->port}" . PHP_EOL;
|
||||||
echo "DO NOT CLOSE THIS TERMINAL " . PHP_EOL;
|
echo "DO NOT CLOSE THIS TERMINAL " . PHP_EOL;
|
||||||
|
|
||||||
// Create WebSocket Server
|
// Create WebSocket Server
|
||||||
$webSock = new ReactServer($SocketDataObj->uri . ':' . $SocketDataObj->port, $loop);
|
$webSock = new ReactServer($SocketDataObj->uri . ':' . $SocketDataObj->port, $loop);
|
||||||
$webSock = new SecureServer($webSock, $loop, $parameters);
|
$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) {
|
$httpServer = function (ServerRequestInterface $request) {
|
||||||
return new Response(
|
return new Response(
|
||||||
200,
|
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."
|
"Socket server is running. SSL is valid."
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle incoming connections and handle HTTP requests
|
// Handle incoming connections and handle HTTP requests
|
||||||
|
// Handle incoming connections and differentiate between WebSocket and HTTP requests
|
||||||
$webSock->on('connection', function ($conn) use ($httpServer) {
|
$webSock->on('connection', function ($conn) use ($httpServer) {
|
||||||
$conn->on('data', function ($data) use ($conn, $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
|
||||||
$response = $httpServer(new ServerRequest('GET', '/'));
|
$headers = [];
|
||||||
|
foreach (explode("\r\n", $data) as $line) {
|
||||||
|
if (strpos($line, ':') !== false) {
|
||||||
|
list($key, $value) = explode(':', $line, 2);
|
||||||
|
$headers[trim($key)] = trim($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Send an HTTP-compliant response back to the client
|
// Check if the Upgrade header is present and indicates a WebSocket connection
|
||||||
$headers = "HTTP/1.1 200 OK\r\n";
|
if (isset($headers['Upgrade']) && strtolower($headers['Upgrade']) === 'websocket') {
|
||||||
$headers .= "Content-Type: text/plain\r\n";
|
// It's a WebSocket connection; do nothing here, as WebSocket connections are handled elsewhere
|
||||||
$headers .= "Content-Length: " . strlen((string) $response->getBody()) . "\r\n";
|
return;
|
||||||
$headers .= "Connection: close\r\n\r\n";
|
} else {
|
||||||
|
// It's an HTTP request, simulate an HTTP request and return a response
|
||||||
|
$response = $httpServer(new ServerRequest('GET', '/'));
|
||||||
|
|
||||||
$conn->write($headers . $response->getBody());
|
// Send an HTTP-compliant response back to the client, with CORS headers included
|
||||||
$conn->end();
|
$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($httpHeaders . $response->getBody());
|
||||||
|
$conn->end();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Create WebSocket server instance
|
// Create WebSocket server instance
|
||||||
$webServer = new IoServer(
|
$webServer = new IoServer(
|
||||||
new HttpServer(
|
new HttpServer(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue