When WebSocket server get message it should run script by path that it gets.
Now I use this scheme:
In WebsocketDaemon:
protected function onMessage($connectionId, $data, $type) {
if (!strlen($data) || !$this->checkFlood($connectionId))
return;
$dataArg = base64_encode($this->getRepackData($connectionId, $data));
$response = shell_exec('php cli/runthroughwebsocket.php '.$dataArg);
$this->sendPacketToClient($connectionId, $response);
}
runthroughwebsocket.php:
<?php
if (php_sapi_name() !== 'cli')
return;
chdir(dirname($_SERVER['SCRIPT_FILENAME']).'/..');
function setData($packet) {
$GLOBALS['ws'] = true;
$_SERVER['PATH_INFO'] = $packet->url;
$_SERVER['HTTP_USER_AGENT'] = $packet->agent;
$_SERVER['REMOTE_ADDR'] = $packet->ip;
foreach ($packet->cookie as $key => $cookie)
$_COOKIE[$key] = $cookie;
}
if (empty($argv[1]))
return;
$packet = json_decode(base64_decode($argv[1]));
setData($packet);
require_once 'index.php';
And it runs like through browser but with parameters that I need. So - it works. But SLOW. It takes few seconds. Even AJAX working faster. I suppose it because of "shell_exec". If not use "shell_exec" and run script by "include" - it fast like bullet, but after second "onMessage" it will write "Cannot redeclare class". And I don't know how to run script in another way. With "require_once" troubles too. Do you have any advice?
UPDATE:
So, I understand that I can't run other scripts in websocket thread. The problem is that my scripts uses global variables like $_SESSION, $_COOKIE, etc. And I can't replace it to local variables. It means that I can't work with users without fork... :-( Is "shell_exec" the fastest (or right) way?
UPDATE again:
I find out that problem is in that line "shell_exec('php ". It runs php.exe process about 2 seconds. How I can it speed up?
I solve the problem!
The problem was in browcap.ini file. Because of it php.exe loads too slow. No more need in re include. "exec()" works fast enough.