douliang6563 2015-04-05 11:59
浏览 49

如何重新包含PHP脚本或运行几次而不分叉/或加速分叉

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.

  • 写回答

1条回答 默认 最新

  • duandu8892 2015-04-05 12:23
    关注

    Move the class to its own class file, where ever you need access to it call it with require_once then instantiate an instance.

    After that you can do your iterations through what is needed because you have your object to work with and you are just reusing the object instead of redeclaring the whole class.

    Update:

    classfile.php

    class classname {
    
        // class methods here.
    
    }
    

    index.php

    require_once 'classfile.php';
    $classobject = new classname;
    
    // You want to do your actions/loops etc here.
    

    Basically include/require the class file rather than the other way around.

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制