Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在使用了server:watch 后,全局变量和常量读取不正常 #3362

Closed
dongasai opened this issue Mar 12, 2021 · 2 comments · Fixed by #3363
Closed

在使用了server:watch 后,全局变量和常量读取不正常 #3362

dongasai opened this issue Mar 12, 2021 · 2 comments · Fixed by #3363
Labels
bug Something isn't working

Comments

@dongasai
Copy link
Contributor

php ./bin/hyperf.php start启动的清空下能够在websocket的控制器内读取到在 ./bin/hyperf.php 中定义的常量,

定义代码如下:

$GLOBALS['GAME_ROOT'] = BASE_PATH.'/server';
define('GAME_ROOT', $GLOBALS['GAME_ROOT']);

读取代码如下


/**
 * Description of WebSocketController
 *
 * @author dongasai
 */
class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
    /**
     * 
     * @param WebSocketServer  $server
     * @param Frame $frame
     * @return void
     */
    public function onMessage($server, Frame $frame): void
    {
        dump('onMessageee', get_class($server), $frame->fd, $frame->data);
        dump(BASE_PATH,GAME_ROOT,$GLOBALS ['GAME_ROOT'] );
    }

    public function onClose($server, int $fd, int $reactorId): void
    {
        var_dump('closed');
    }

    public function onOpen($server, Request $request): void
    {
        $server->push($request->fd, json_encode([
            "p" => uniqid(),
            'd' => [],
            's' => 'ws',
            'r' => 'open'
        ]));
    }

}

输出内容如下:

^ "onMessageee"
^ "Swoole\WebSocket\Server"
^ 1
^ "{"s":"ws","r":"ping","d":{},"p":"d300e41c774056c8bb9abed5d87585ad"}"
^ "/var/www/html"
^ "/var/www/html/server"
^ "/var/www/html/server"
^ "onMessageee"
^ "Swoole\WebSocket\Server"

在使用php ./bin/hyperf.php server:watch启动时输出内容如下

输出内容如下

^ "onMessageee"
^ "Swoole\WebSocket\Server"
^ 1
^ "{"s":"ws","r":"ping","d":{},"p":"ed8b48cbbf3b60dbc3ec324c22b45bd7"}"

Warning: Use of undefined constant GAME_ROOT - assumed 'GAME_ROOT' (this will throw an Error in a future version of PHP) in /var/www/html/app/Controller/WebSocketController.php on line 29

Notice: Undefined index: GAME_ROOT in /var/www/html/app/Controller/WebSocketController.php on line 29
^ "/var/www/html"
^ "GAME_ROOT"
^ null


@limingxinleo limingxinleo added the bug Something isn't working label Mar 12, 2021
@limingxinleo
Copy link
Member

这个问题我知道,抽空改。。。

@limingxinleo
Copy link
Member

直接从 pr 里覆盖一下 watcher.php 的代码,试试看

如果 ok 我就合并代码

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants