我正在使用websocket向所有连接的用户发送广播消息。
我的查询是可以从任何其他PHP文件调用onMessage()
函数
onMessage()
函数位于Chat
类。
我主要担心的是从我的服务器外部更新数据库(从任何地方使用同步)然后我想将这些新更改发送给所有服务器连接的用户。
让我知道任何方式,所以我可以做到。
这是rachet websocket
的 Chat.php 文件 namespace Chat;
use Chat\Repository\ChatRepository;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $repository;
public function __construct() {
$this->repository = new ChatRepository;
}
public function onOpen(ConnectionInterface $conn) {
conection open code..
}
public function onClose(ConnectionInterface $conn) {
$this->repository->removeClient($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
error code...
}
public function onMessage(ConnectionInterface $conn, $msg) {
$data = $this->parseMessage($msg);
foreach ($this->repository->getClients() as $client) {
$client->sendMsg($data->msg);
}
}
private function parseMessage($msg) {
return json_decode($msg);
}
}
我正在使用codeigniter,所以上面是库类文件。
现在我想从另一个控制器文件中调用此onMessage()
。
我可以做吗 ?
如果是,那么如何在控制器文件中获得connectionInterface
?