1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 10:49:36 +02:00
and Socket improvements
This commit is contained in:
DanielnetoDotCom 2021-01-15 10:03:09 -03:00
parent e00c0d7022
commit ee3fabaad8
10 changed files with 225 additions and 63 deletions

View file

@ -4579,6 +4579,16 @@ function forbiddenPage($message, $logMessage = false) {
exit;
}
function videoNotFound($message, $logMessage = false) {
global $global;
$_REQUEST['404ErrorMsg'] = $message;
if ($logMessage) {
_error_log($message);
}
include $global['systemRootPath'] . 'view/videoNotFound.php';
exit;
}
function isForbidden() {
global $global;
if (!empty($global['isForbidden'])) {
@ -5289,7 +5299,11 @@ function getCurrentTheme() {
return $config->getTheme();
}
function sendSocketMessage($msg, $callbackJSFunction="", $users_id=""){
/*
* $users_id="" or 0 means send messages to all users
* $users_id="-1" means send to no one
*/
function sendSocketMessage($msg, $callbackJSFunction="", $users_id="-1"){
if(AVideoPlugin::isEnabledByName('Socket')){
if(!is_string($msg)){
$msg = json_encode($msg);
@ -5304,5 +5318,21 @@ function sendSocketMessage($msg, $callbackJSFunction="", $users_id=""){
}
function sendSocketMessageToUsers_id($msg, $users_id, $callbackJSFunction=""){
return sendSocketMessage($msg, $callbackJSFunction, $users_id);
if(!is_array($users_id)){
$users_id = array($users_id);
}
$resp = array();
foreach ($users_id as $value) {
$resp[] = sendSocketMessage($msg, $callbackJSFunction, $value);
}
return $resp;
}
function sendSocketMessageToAll($msg, $callbackJSFunction=""){
return sendSocketMessage($msg, $callbackJSFunction, "");
}
function sendSocketMessageToNone($msg, $callbackJSFunction=""){
return sendSocketMessage($msg, $callbackJSFunction, -1);
}

View file

@ -1,13 +1,15 @@
<?php
namespace Socket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require_once dirname(__FILE__) . '/../../videos/configuration.php';
require_once $global['systemRootPath'] . 'plugin/Socket/functions.php';
class Message implements MessageComponentInterface {
protected $clients, $clients_users_id;
public function __construct() {
@ -20,32 +22,48 @@ class Message implements MessageComponentInterface {
_log_message("New connection! ({$conn->resourceId})");
// Store the new connection to send messages to later
//$this->clients->attach($conn);
if(!isset($this->clients[$conn->resourceId] )){
if (!isset($this->clients[$conn->resourceId])) {
$this->clients[$conn->resourceId] = array();
}
$this->clients[$conn->resourceId]['conn'] = $conn;
self::msgToUser($conn->resourceId, $conn->resourceId);
self::msgToResourceId("Connection opened", $conn->resourceId);
}
public function msgToUser($msg, $id) {
$this->clients[$id]['conn']->send($msg);
public function msgToResourceId($msg, $resourceId) {
if(!is_array($msg)){
$this->msgToArray($msg);
}
$msg['ResourceId'] = $resourceId;
$msg = json_encode($msg);
_log_message("msgToUser: resourceId=({$resourceId}) {$msg}");
$this->clients[$resourceId]['conn']->send($msg);
}
public function msgToUsers_id($msg, $users_id) {
if(!empty($this->clients_users_id[$users_id])){
$this->msgToUser($msg, $this->clients_users_id[$users_id]);
if(empty($users_id)){
return false;
}
$this->clients[$from->resourceId]['users_id'];
$count = 0;
foreach ($this->clients as $resourceId => $value) {
if($value['users_id'] == $users_id){
$count++;
$this->msgToResourceId($msg, $resourceId);
}
}
_log_message("msgToUsers_id: sent to ($count) clients users_id={$users_id}");
}
public function msgToAll(ConnectionInterface $from, $msg) {
_log_message("msgToAll");
$numRecv = count($this->clients) - 1;
_log_message(sprintf('Connection %d sending message "%s" to %d other connection%s'.PHP_EOL
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's'));
//_log_message(sprintf('Connection %d sending message "%s" to %d other connection%s' . PHP_EOL , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's'));
foreach ($this->clients as $client) {
foreach ($this->clients as $key => $client) {
if ($from !== $client['conn']) {
// The sender is not the receiver, send to each client connected
$client['conn']->send($msg);
$this->msgToResourceId($msg, $key);
}
}
}
@ -53,41 +71,67 @@ class Message implements MessageComponentInterface {
public function onMessage(ConnectionInterface $from, $msg) {
_log_message("onMessage: {$msg}");
$json = json_decode($msg);
if(empty($json)){
if (empty($json)) {
_log_message("onMessage ERROR: JSON is empty ");
return false;
}
if(empty($json->webSocketToken)){
if (empty($json->webSocketToken)) {
_log_message("onMessage ERROR: webSocketToken is empty ");
return false;
}
if(!$msgObj = getDecryptedInfo($json->webSocketToken)){
if (!$msgObj = getDecryptedInfo($json->webSocketToken)) {
_log_message("onMessage ERROR: could not decrypt webSocketToken");
return false;
}
if($msg == "webSocketToken" && empty($this->clients[$from->resourceId]['users_id'])){
switch ($json->msg) {
case "webSocketToken":
if (empty($this->clients[$from->resourceId]['users_id'])) {
_log_message("onMessage: set users_id {$msgObj->users_id}");
$this->clients[$from->resourceId]['users_id'] = $msgObj->users_id;
if(!isset($this->clients_users_id)){
$this->clients_users_id = array();
}
$this->clients_users_id[$msgObj->users_id] = $from->resourceId;
break;
default:
$this->msgToArray($json);
if (!empty($json['to_users_id'])) {
$this->msgToUsers_id($json['msg'], $json['to_users_id']);
} else {
$this->msgToAll($from, $json['msg']);
}
break;
}
}
private function msgToArray(&$json){
$json = $this->makeSureIsArray($json);
$msg = $this->makeSureIsArray(@$json['msg']);
$msg['callback'] = @$json['callback'];
$msg['uniqid'] = uniqid();
$json['msg'] = $msg;
return true;
}
private function makeSureIsArray($msg){
if(empty($msg)){
return array();
}
if(is_string($msg)){
$decoded = json_decode($msg);
}else{
if(!empty($json->users_id)){
$this->msgToUsers_id($from, $json->users_id);
}else{
$this->msgToAll($from, $msg);
$decoded = object_to_array($msg);
}
if(is_string($msg) && !$decoded){
return array($msg);
}else if(is_string($msg)){
return object_to_array($decoded);
}
return object_to_array($msg);
}
public function onClose(ConnectionInterface $conn) {
_log_message("Connection {$conn->resourceId} has disconnected");
// The connection is closed, remove it, as we can no longer send it messages
//$this->clients->detach($conn);
if(!empty($this->clients[$conn->resourceId]) && !empty($this->clients[$conn->resourceId]['users_id'])){
unset($this->clients_users_id[$this->clients[$conn->resourceId]['users_id']]);
}
unset($this->clients[$conn->resourceId]);
}
@ -99,9 +143,10 @@ class Message implements MessageComponentInterface {
public function getTags() {
return array('free', 'live');
}
}
function _log_message($msg){
function _log_message($msg) {
_error_log($msg);
echo $msg.PHP_EOL;
echo $msg . PHP_EOL;
}

View file

@ -63,6 +63,9 @@ class Socket extends PluginAbstract {
public static function getSocketJS() {
global $global;
$socketobj = AVideoPlugin::getDataObject("Socket");
$address = parse_url($global['webSiteRootURL'], PHP_URL_HOST);
$port = $socketobj->port;
include $global['systemRootPath'] . 'plugin/Socket/footer.php';
}
@ -80,7 +83,7 @@ class Socket extends PluginAbstract {
$SocketSendObj->webSocketToken = getEncryptedInfo();
$SocketSendObj->msg = $msg;
$SocketSendObj->json = json_decode($msg);
$SocketSendObj->users_id = $users_id;
$SocketSendObj->to_users_id = $users_id;
$SocketSendObj->callback = $callbackJSFunction;
$obj = new stdClass();

View file

@ -1,5 +1,4 @@
<script>
var webSocketURL = 'ws://localhost:8888';
var webSocketToken = '<?php echo getEncryptedInfo(); ?>';
var webSocketURL = '<?php echo "ws:{$address}:{$port}"?>';
</script>
<script src="<?php echo $global['webSiteRootURL']; ?>plugin/Socket/script.js" type="text/javascript"></script>

View file

@ -1,28 +1,35 @@
var socketMyResourceId = 0;
var socketConnectRequested = 0;
function socketConnect() {
if(socketConnectRequested){
return false;
}
socketConnectRequested = 1;
console.log('Trying to reconnect on ' + webSocketURL);
conn = new WebSocket(webSocketURL);
conn.onopen = function (e) {
console.log("Socket onopen", e);
socketMyResourceId = 0;
sendSocketMessage("webSocketToken", "");
sendSocketMessageToNone("webSocketToken", "");
return false;
};
conn.onmessage = function (e) {
console.log("Socket onmessage", e);
//console.log("Socket onmessage", e);
var json = JSON.parse(e.data);
console.log("Socket onmessage", json);
if (!socketMyResourceId) {
socketMyResourceId = parseInt(e.data);
socketMyResourceId = parseInt(json.ResourceId);
var msg = "Socket socketMyResourceId " + socketMyResourceId;
sendSocketMessage(msg, "");
//sendSocketMessage(msg, "", "");
console.log(msg);
} else {
var json = JSON.parse(e.data);
var myfunc;
if (json.callback) {
var code = "if(typeof " + json.callback + " == 'function'){myfunc = " + json.callback + ";}else{myfunc = defaultCallback;}";
console.log(code);
//console.log(code);
eval(code);
} else {
console.log("onmessage: callback not found");
myfunc = defaultCallback;
}
myfunc(json);
@ -30,6 +37,7 @@ function socketConnect() {
};
conn.onclose = function (e) {
socketConnectRequested = 0;
console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason);
setTimeout(function () {
socketConnect();
@ -37,18 +45,27 @@ function socketConnect() {
};
conn.onerror = function (err) {
socketConnectRequested = 0;
console.error('Socket encountered error: ', err.message, 'Closing socket');
conn.close();
};
}
function sendSocketMessage(msg, callback) {
function sendSocketMessageToAll(msg, callback) {
sendSocketMessageToUser(msg, callback, "");
}
function sendSocketMessageToNone(msg, callback) {
sendSocketMessageToUser(msg, callback, -1);
}
function sendSocketMessageToUser(msg, callback, users_id) {
if (conn.readyState === 1) {
conn.send(JSON.stringify({msg: msg, webSocketToken: webSocketToken, callback: callback}));
conn.send(JSON.stringify({msg: msg, webSocketToken: webSocketToken, callback: callback, users_id, users_id}));
} else {
console.log('Socket not ready send message in 1 second');
setTimeout(function () {
sendSocketMessage(msg, callback);
sendSocketMessageToUser(msg, callback, to_users_id);
}, 1000);
}
}
@ -69,10 +86,9 @@ function isSocketActive(){
}
function defaultCallback(json) {
console.log('defaultCallback', json);
//console.log('defaultCallback', json);
}
$(function () {
socketConnect();
sendSocketMessage("test", "");
});

View file

@ -44,6 +44,11 @@ if(!empty($metaDescription)){
// for SEO to not rise an error of duplicated title or description of same pages with and without last slash
$metaDescription .= getSEOComplement(array("addAutoPrefix" => false));
$theme = getCurrentTheme();
if(empty($config)){
$config = new Configuration();
}
?>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

View file

@ -73,6 +73,9 @@ if (!empty($evideo)) {
}
$videosArrayId = PlayList::getVideosIdFromPlaylist($playlist_id);
if(empty($videosArrayId)){
videoNotFound(__('Playlist is empty or does not exist'));
}
$videosPlayList = Video::getAllVideos("viewable", false, false, $videosArrayId, false, true);
$videosPlayList = PlayList::sortVideos($videosPlayList, $videosArrayId);

View file

@ -135,10 +135,10 @@ $modeYouTubeTime = microtime(true);
<script>
var fading = false;
var autoPlaySources = <?php echo json_encode($autoPlaySources); ?>;
var autoPlayURL = '<?php echo $autoPlayURL; ?>';
var autoPlayPoster = '<?php echo $autoPlayPoster; ?>';
var autoPlayThumbsSprit = '<?php echo $autoPlayThumbsSprit; ?>';
var autoPlaySources = <?php echo json_encode(@$autoPlaySources); ?>;
var autoPlayURL = '<?php echo @$autoPlayURL; ?>';
var autoPlayPoster = '<?php echo @$autoPlayPoster; ?>';
var autoPlayThumbsSprit = '<?php echo @$autoPlayThumbsSprit; ?>';
function showAutoPlayVideoDiv() {
var auto = $("#autoplay").prop('checked');

View file

@ -21,7 +21,9 @@
$vType = Video::getIncludeType($video);
require "{$global['systemRootPath']}view/include/{$vType}.php";
$modeYouTubeTimeLog['After include video ' . $vType] = microtime(true) - $modeYouTubeTime;
if(!empty($modeYouTubeTime)){
$modeYouTubeTimeLog['After include video ' . $vType] = microtime(true) - $modeYouTubeTime;
}
$modeYouTubeTime = microtime(true);
?>
<div class="row">

59
view/videoNotFound.php Normal file
View file

@ -0,0 +1,59 @@
<?php
if (!isset($global['systemRootPath'])) {
require_once '../videos/configuration.php';
}
header('HTTP/1.0 404 Not Found', true, 404);
$img = "{$global['webSiteRootURL']}view/img/this-video-is-not-available.jpg";
$poster = "{$global['webSiteRootURL']}view/img/this-video-is-not-available.jpg";
$imgw = 1280;
$imgh = 720;
unset($_SESSION['type']);
session_write_close();
$video = array();
$video['id'] = 0;
$video['type'] = 'notfound';
$video['rotation'] = 0;
$video['videoLink'] = "";
$video['title'] = __("Video Not Available");
$video['clean_title'] = "video-not-available";
$video['description'] = "";
$video['duration'] = "";
$video['creator'] = "";
$video['likes'] = "";
$video['dislikes'] = "";
$video['category'] = "embed";
$video['views_count'] = 0;
$video['filename'] = "";
?>
<!DOCTYPE html>
<html lang="<?php echo $_SESSION['language']; ?>">
<head>
<title><?php echo __('Video Not Found'); ?></title>
<link href="<?php echo $global['webSiteRootURL']; ?>plugin/Gallery/style.css" rel="stylesheet" type="text/css"/>
<?php
include $global['systemRootPath'] . 'view/include/head.php';
?>
</head>
<body class="<?php echo $global['bodyClass']; ?>">
<?php include $global['systemRootPath'] . 'view/include/navbar.php'; ?>
<div class="container-fluid principalContainer" id="modeYoutubePrincipal">
<?php
require "{$global['systemRootPath']}view/modeYoutubeBundle.php";
?>
</div>
<?php
include $global['systemRootPath'] . 'view/include/footer.php';
showCloseButton();
?>
<script>
$(function () {
<?php
if(!empty($_REQUEST['404ErrorMsg'])){
echo "avideoAlertInfo(\"{$_REQUEST['404ErrorMsg']}\");";
}
?>
});
</script>
</body>
</html>