Jump to content

[PHP>=5.3.0]Get server infos of Simple Radio Standalone


Ergo

Recommended Posts

Hi all,

 

My virtual web school use the Simple Radio Standalone server.

I made a PHP Class to get informations of SRS Server to display it on my web site.

(Exemple of use : http://niprod.net/etat , you can click on version label to see server details)

 

I give here the PHP Class, for you usage. ( CC-BY-NC-SA )

 

To use it, it's realy simple, exemple :

 

<?php

include('SRSClient.php');

$srs = new SRSClient('xx.xx.xx.xx',5004) ; 
if ($srs->connect()->isConnect())
{
$srs->waitResponse() ; 
echo '<h1>Serveur SRS</h1><pre>' ; 
echo 'Version : '. $srs->getVersion() . "\n"; 
echo 'Clients count : '. (count($srs->getClients()) - 1) . "\n"; // do not count me 
echo 'Serveur settings : ' . "\n" ; 
foreach ($srs->getServerSettings() as $key => $value) {
	echo "\t" . $key . ' : ' . ($value ? 'ON' : 'OFF') . "\n"; 
};
echo '</pre>';
$srs->disconnect();
}
else
{
echo 'Connection failed' ; 
}

 

And page display :

 

attachment.php?attachmentid=192152&stc=1&d=1534409860

 

Be careful : Without connection of SRS Server, the script wait socket_timeout (php setting default : 30sec). I advise to make this script in cron task, and saved return in a file/database. And your website get information in this file/website.

 

The PHP Class code :

 

<?php 

/*************** SRSClient.php
 * Simple Library for SRS (Simple Radio Standalone)
 * v1.0
 * Author : "Ergo" Nicolas Dumas 2018 (http://niprod.net)
 * Licence : Creative Commun - CC-BY-NC-SA (https://creativecommons.org/licenses/by-nc-sa/4.0/)
 * compatibility : SRS Server 1.5.3.x
**/


class SRSClient
{
private $_socket = false ; 
private $_ip ; 
private $_port ; 
private $_timeout = 5 ;
private $_uuid ;
private $_response = array (
	'Client' => NULL,
	'MsgType' => NULL,
	'Clients' => NULL,
	'ServerSettings' => NULL,
	'ExternalAWACSModePassword' => NULL,
	'Version' => NULL
); 
public static $msgType = array(
	'UPDATE' => 0, //META Data update - No Radio Information
       'PING' => 1,
       'SYNC' => 2,
       'RADIO_UPDATE' => 3, //Only received server side
       'SERVER_SETTINGS' => 4,
       'CLIENT_DISCONNECT' => 5, // Client disconnected
       'VERSION_MISMATCH' => 6,
       'EXTERNAL_AWACS_MODE_PASSWORD' => 7, // Received server side to "authenticate"/pick side for external AWACS mode
       'EXTERNAL_AWACS_MODE_DISCONNECT' => 8 // Received server side on "voluntary" disconnect by the client (without closing the server connection)
) ;
private $_clientName = 'PHP SRS Client' ; 
private $_version = '1.5.3.5' ; 

/**
  * Construct function
  *	@string $ip : need ip or internet adresse of SRS Server
  * @int  $port : need port number
  */
public function __construct($ip, $port=5002)
{
	if ($port < 0 or $port > 65535) 
		throw new InvalidArgumentException('Port must be betwwen 0 and 65535, given : '.$port);
	if (!is_string($ip)) 
		throw new InvalidArgumentException('Ip must be a string, given : '.gettype($ip));
	$this->_port = $port ; 
	$this->_ip = $ip ; 
	$this->_uuid = uniqid('php_srsClient_') ; //add specific prefix for srsServer
	return $this ; 
}

public function isConnect() 
{
	return ($this->_socket !== false) ; 
}

public function connect()
{
	// already connected ? 
	if ($this->isConnect()) $this->disconnect() ; 
	$this->_socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if (!$this->isConnect()) return $this ;
	$result = @socket_connect($this->_socket, $this->_ip, $this->_port);
	if ($result === false )
	{
		@fclose($this->_socket) ;
		$this->_socket = false ; 
		return $this ; 
	}
	$this->send($this->createMsg(self::$msgType['SYNC'])) ; 
	return $this ; 
}

public function disconnect()
{
	// not connected ? nothing to do.
	if (! $this->isConnect()) return $this ; 
	// send disconnect information : 
	socket_close($this->_socket); 
	$this->_socket = false ; 
}

public function waitResponse()
{
	$out = @socket_read($this->_socket, 1,PHP_BINARY_READ);
	$response = '';
	while ($out != "\n") {
		$response .= $out ; 
	    $out = @socket_read($this->_socket, 1,PHP_BINARY_READ);
	}
	$this->_response = $this->decode_msg($response);
	return $this ; 
}

public function getMsgTypeResponse()
{
	if (! $this->isConnect()) return false ; 
	return $this->_response['MsgType'] ; 
}

public function getVersion()
{
	if (! $this->isConnect()) return false ; 
	return $this->_response['Version'] ; 
}

public function getClients()
{
	if (! $this->isConnect()) return false ; 
	return $this->_response['Clients'] ; 
}

public function getServerSettings()
{
	if (! $this->isConnect()) return false ; 
	return $this->_response['ServerSettings'] ; 
}

/**
  * Destruct function
  * need to close connection with SRS Client
  */
public function __destruct()
{
	if ($this->isConnect()) $this->disconnect();
	}

	private function recusive_str2Bool_array($array)
	{
		if (!is_array($array))
		{
			if (is_string($array))
			{
				if (strtolower($array) == 'false') $array = false ;
				if (strtolower($array) == 'true') $array = true ;
			}
		}
		else
		{
			$a = array() ;
			foreach ($array as $key => $value) 
			{
				$a[$key] = $this->recusive_str2Bool_array($value) ; 
			}
			$array = $a ; 
		}
		return $array ; 
	}

	private function recusive_bool2Str_array($array)
	{
		if (!is_array($array))
		{
			if (is_bool($array))
			{
				if ($array)
				{
					$array = 'True' ;
				} 
				else
				{
					$array = 'False' ;
				}
			}
		}
		else
		{
			foreach ($array as $key => $value) 
			{
				$array[$key] = $this->recusive_bool2Str_array($value) ; 
			}
		}
		return $array ; 
	}

	private function encode_msg($msg)
	{
		return json_encode($this->recusive_bool2Str_array($msg));
	}

	private function decode_msg($msg)
	{
		return $this->recusive_str2Bool_array(json_decode($msg, true));
	}

	private function send($msg)
	{
		// not connected ? nothing to do.
	if (! $this->isConnect()) return $this ; 
		$jsonMsg = $this->encode_msg($msg) . "\n"; 
		@socket_write($this->_socket, $jsonMsg, strlen($jsonMsg));
		return $this ; 
	} 

	private function createMsg($msgType)
	{
		if ($msgType < 0 or $msgType >= count(self::$msgType))
			throw new InvalidArgumentException('msgType must be betwwen 0 and '.count(self::$msgType).', given : '.$msgType);
		$msg = array (
		'Client' => array (
			'ClientGuid' => $this->_uuid,
			'Name' => $this->_clientName,
			'Coalition' => 0,
			'RadioInfo' => NULL,
			'Position' => 
			array ( 'x' => 0, 'y' => 0, 'z' => 0 ),
			'ClientChannelId' => NULL,
		),
		'MsgType' => $msgType,
		'Clients' => $this->_response['Clients'],
		'ServerSettings' => $this->_response['ServerSettings'],
		'ExternalAWACSModePassword' => $this->_response['ExternalAWACSModePassword'],
		'Version' => $this->_version
	) ; 
	return $msg ; 
	}
}

179783474_Capturedecran2018-08-16a10_57_12.jpg.34f34cadeee6439328c129abe37ded1b.jpg

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...