gpt4 book ai didi

php - Ratchet Websocket 无法从客户端接收数据?

转载 作者:行者123 更新时间:2023-12-04 16:13:53 25 4
gpt4 key购买 nike

我有一个带有frameWork Symfony 的实时Web 应用程序。我需要将数据从客户端发送到 websket 服务器。所以我尝试了这个:

var conn = new WebSocket('ws://127.0.0.1:8080');
console.log (conn);
conn.onopen = function (e) {
console.log ("Connection established!");
conn.send("xoxo");

};

它没有显示任何错误,在服务器端我有这个:
服务器代码:
$app=new AggregateApplication();
$loop = \React\EventLoop\Factory::create();
$context = new \React\ZMQ\Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($app, 'EditMessage'));
$webSock = new \React\Socket\Server($loop);
$webSock->listen(8080, '127.0.0.1');
$handler = $this->getContainer()->get('session.handler');
$server=new \Ratchet\Wamp\WampServer($app);
$server = new SessionProvider($server, $handler);
$webServer = new \Ratchet\Server\IoServer(new \Ratchet\WebSocket\WsServer($server),$webSock);
$loop->run();

这是我的应用程序代码:
class AggregateApplication implements WampServerInterface {
protected $clients;
protected $comming;
public function __construct() {
$this->clients = array();
$this->comming = array();
}

public function onOpen(ConnectionInterface $conn){
$this->clients[array_shift($this->comming)]=$conn;
echo "New connection! ".array_shift($this->comming)." ({$conn->resourceId})\n";
}
public function onCall(ConnectionInterface $conn, $id, $topic, array $params){

}
public function onSubscribe(ConnectionInterface $conn, $topic){
echo "onSubscribe";
}
public function onUnSubscribe(ConnectionInterface $conn, $topic){

}
public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible){

}
public function onClose(ConnectionInterface $conn) {
unset($this->clients[array_search($conn, $this->clients)]);
echo 'close connection ';
}

public function onError(ConnectionInterface $conn, \Exception $e) {
}
}

我找不到的问题是我会捕捉到客户端发送的消息吗?

最佳答案

您的留言不符合WAMP standard .看看AutobahnJS用于您的客户端连接。也看看Ratchet Push Integration Tutorial ,其中有一个功能示例。

关于php - Ratchet Websocket 无法从客户端接收数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17893586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com