使用workerman转发消息遇到的作用域问题?

参考的demo

https://github.com/walkor/wor...

业务场景

1.Events.php 调用 Server.php方法 (因为Events里面是静态的,不太会用,所以新建了一个类)
2.Server.php方法连接的时候创建AsyncTcpConnection客户端连接其他的服务端
3.客户端收到消息后通过Server发送给网页
4.( 网页发送消息给server, serverAsyncTcpConnection创建的客户端发送给其他服务端

代码

在上面的例子上,我新增了 Server.phpClient.php 用于消息转发

clipboard.png

Events.php

我把Server保存在$_SESSION里,这样不知道有没有问题? = =

public static function onMessage($client_id, $data)
{
    $_SESSION['Server']->onMessage($client_id, $data);
}
 
public static function onConnect($client_id)
{
    $_SESSION['client_id'] = $client_id;
    $_SESSION['Server'] = new Server();
    $_SESSION['Server']->onConnect($client_id);
}

Server.php

我把Client保存在$_SESSION里,这样不知道有没有问题? = = !

public function onConnect($client_id)
{
    $serverIp = '***.***.***.***';
    $serve = "ws://$serverIp:9999";
    $_SESSION['ClientAsyncTcpConnection'] = new AsyncTcpConnection($serve);
    $_SESSION['Client'] = new Client($client_id);
    $_SESSION['Client']->serveClientId = $client_id;
    $_SESSION['Client']->serverIp = $serverIp;
    $_SESSION['ClientAsyncTcpConnection']->onConnect = array($_SESSION['Client'], 'onConnect');
    $_SESSION['ClientAsyncTcpConnection']->onMessage = array($_SESSION['Client'], 'onMessage');
    $_SESSION['ClientAsyncTcpConnection']->onError = array($_SESSION['Client'], 'onError');
    $_SESSION['ClientAsyncTcpConnection']->onClose = array($_SESSION['Client'], 'onClose');
    $_SESSION['ClientAsyncTcpConnection']->onError = array($_SESSION['Client'], 'onError');
    $_SESSION['ClientAsyncTcpConnection']->connect();
}
public function onMessage($client_id, $data)
{
    $_SESSION['Client']->onRecv($data);
}

Client.php

这里遇到点问题,但是不知道是哪里出的问题

function onConnect($con)
{
    $this->mCon = $con;
}
public function onRecv($data)
{
    var_dump($this->mCon);
}

请问在 Client.php 里面 var_dump($this->mCon)为什么是NULL呢?

阅读 1.9k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题