I created a simple bash file to run a php file like this:
ws.sh
!#/bin/bash
php -q /var/www/htdocs/server.php
server.php
<?php
// prevent the server from timing out
set_time_limit(0);
session_start();
// include the web sockets server script (the server is started at the far bottom of this file)
require 'class.PHPWebSocket.php';
// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
// check if message length is 0
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}
$_SESSION['currentId'] = $message;
}
// when a client connects
function wsOnOpen($clientID){
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "$ip ($clientID) has connected. ");
$data = array('connectionId' => $clientID,'msg'=>'Connected');
$Server->wsSend($clientID, json_encode($data));
}
// when a client closes or lost connection
function wsOnClose($clientID, $status) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "$ip ($clientID) has disconnected. sess = ".$session_id );
}
// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
$Server->wsStartServer('server-ip-here', 81);
?>
then on my terminal I will run the bash file into background
./ws.sh &
Now, the problem is if the bash file running on the background the client was not able to connect to my websocket and returns an error "CONNECTION REFUSED" but if its a normal running (not background) I have to problem connecting to it. Any idea? Please help. Thank you!