I'm trying to start a WebSocket server within threads, but it does not work fine. It runs with no error, the client get connected to the server, but it disconnects immediately. Does anyone know why? That's my code:
<?php
require 'websocket.class.php';
class AsyncOperation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server;
$Server->wsSend($clientID, "Message received. Just any response back to see if it is working...");
}
public function wsOnOpen($clientID)
{
global $Server;
$Server->wsSend($clientID, "client $clientID is connected");
}
public function run() {
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
$Server->wsStartServer('localhost', 9300);
}
}
//These data are gonna be used later - not important now
$charts = array(
array("name" => "Blablabla")
);
$stack = array();
// Initiate Multiple Thread
foreach ($charts as $c) {
$stack[] = new AsyncOperation($c);
}
// Start The Threads
foreach ($stack as $t) {
$t->start();
}
The expected would be the client kept connected, and when the client send a message, receive back "Message received. Just any response back to see if it is working...", but it does not happen.