我有一个带有frameWork Symfony的实时web应用程序。我需要将数据从客户端发送到webpacket服务器。所以我尝试了这个:
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 = ReactEventLoopFactory::create();
$context = new ReactZMQContext($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($app, 'EditMessage'));
$webSock = new ReactSocketServer($loop);
$webSock->listen(8080, '127.0.0.1');
$handler = $this->getContainer()->get('session.handler');
$server=new RatchetWampWampServer($app);
$server = new SessionProvider($server, $handler);
$webServer = new RatchetServerIoServer(new RatchetWebSocketWsServer($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标准。查看您的客户端连接的AutobahnJS。另请参阅Ratchet Push Integration教程,其中有一个功能示例。
请记住,WAMP标准只是Push Integration(ZMQ)的一个建议。您可以通过纯javascript在客户端处理此机制,而无需使用任何库,如AutobahnJS
服务器代码:
// Create loop for listen
$loop = ReactEventLoopFactory::create();
$pusher = new Pusher;
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new ReactZMQContext($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new ReactSocketServer($loop);
$webSock->listen(8080, '127.0.0.1'); // Binding to 127.0.0.1 means remotes can connect
$webServer = new RatchetServerIoServer(
new RatchetWebSocketWsServer(
$pusher
), $webSock
);
// run server for listen websocket base connection
$loop->run();
您的应用程序代码不需要主题类(WAMP类)用于消息模式,因此不需要使用WAMP和AutobahnJS。
例如应用程序代码:
class Pusher implements MessageComponentInterface {
public function __construct() {
$this->clients = new SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {}
public function onMessage(ConnectionInterface $from, $msg) {}
public function onClose(ConnectionInterface $conn) {}
public function onError(ConnectionInterface $conn, Exception $e) {}
public function onBlogEntry($entry) {}
}
观看下面的幻灯片。
http://wamp-proto.org/
客户端可以通过远程过程调用(RPC)与服务器进行通信。
如果您使用高速公路JS(http://autobahn.ws/js/),您可以将其添加到客户端中。
session.call('myaction', [mydata]).then(
function (res) {
console.log("Result:", res);
}
);
在Pusher类中,您已经使用以下函数捕获从客户端发送的消息。
public function onCall(ConnectionInterface $conn, $id, $topic, array $params)
{
// do what you want
}