FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Socket.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Socket extends Component{
6 
7  public $clientHost = null; //!< Host the WebSocket server is running on (for public connections; empty = same as HTTP).
8  public $clientPort = null; //!< Port client websockets can connect to (empty = no connection allowed).
9  public $clientPrefix = null; //!< Prefix for websocket requests (for example when tunneling WSS traffic).
10  public $clientLocalPort = null; //!< When the WebSocket server should listen on another local port then the client connects
11  // to (for example when tunneling WSS traffic).
12  public $clientOrigins = []; //!< Allowed origins (empty = all).
13  public $tokenTtl = 30; //!< Time a request token is valid (seconds).
14 
15  public $localHost = 'localhost'; //!< Host the WebSocket server is running on (for local connections).
16  public $localPort = 6871; //!< Port for local connections (HTTP server <-> WebSocket server).
17  public $localClients = ['127.0.0.1','::1']; //!< Allowed clients for a local connection.
18 
19  protected $_server = null; //!< WebSocket server.
20 
21  /**
22  * Request an action from the HTTP Server on the WebSocket server.
23  * @param string $controller_name Controler to apply the action on (empty = local action).
24  * @param string $action Requested action.
25  * @param mixed $params Parameters for the action.
26  * @param int $read_length Maximum number of bytes to read.
27  * @return string Response from the WebSocket server (false on error).
28  * @see http://php.net/socket_last_error
29  */
30  public function action($controller_name,$action,$params = null,$read_length = 1024){
31  $result = false;
32  if($this->localPort && ($socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP))) try{
33  if(
34  socket_connect($socket,$this->localHost,$this->localPort) &&
35  (socket_write($socket,serialize(array_merge($params ?: [],['controller' => $controller_name,'action' => $action]))) !== false)
36  ) $result = socket_read($socket,$read_length);
37  socket_close($socket);
38  }
39  catch(\Exception $e){
40  $this->component('log')->emergency('Can not connect to socket server: ' . $e->getMessage(),__FILE__,__LINE__);
41  }
42  return $result ? unserialize($result) : $result;
43  }
44  /**
45  * Create a WebSocket access token.
46  * @param array $params Parameters to pass to the WebSocket server (this is used as the session - read-only).
47  * @return string Token (false on error; null = not available).
48  */
49  public function token($params = null){
50  if(!$this->clientPort) return null;
51  $params = array_merge($params ?: [],['token' => $token = \Rsi\Str::random(),'sessionId' => session_id()]);
52  return $this->action(null,'token',$params) === false ? false : $token;
53  }
54 
55  protected function getServer(){
56  if(!$this->_server) $this->_server = new Socket\Server($this);
57  return $this->_server;
58  }
59 
60 }
$clientLocalPort
When the WebSocket server should listen on another local port then the client connects.
Definition: Socket.php:10
$clientPort
Port client websockets can connect to (empty = no connection allowed).
Definition: Socket.php:8
$clientOrigins
Allowed origins (empty = all).
Definition: Socket.php:12
$clientPrefix
Prefix for websocket requests (for example when tunneling WSS traffic).
Definition: Socket.php:9
$localClients
Allowed clients for a local connection.
Definition: Socket.php:17
$localPort
Port for local connections (HTTP server <-> WebSocket server).
Definition: Socket.php:16
action($controller_name, $action, $params=null, $read_length=1024)
Request an action from the HTTP Server on the WebSocket server.
Definition: Socket.php:30
$clientHost
Host the WebSocket server is running on (for public connections; empty = same as HTTP).
Definition: Socket.php:7
$localHost
Host the WebSocket server is running on (for local connections).
Definition: Socket.php:15
$_server
WebSocket server.
Definition: Socket.php:19
Basic component class.
Definition: Component.php:8
token($params=null)
Create a WebSocket access token.
Definition: Socket.php:49
$tokenTtl
Time a request token is valid (seconds).
Definition: Socket.php:13
component($name)
Get a component (local or default).
Definition: Component.php:80