首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用composer cboden/ratchet(websocket),服务端怎么接收发来的内容?

使用composer cboden/ratchet(websocket),服务端怎么接收发来的内容?

提问于 2018-11-16 16:06:11
回答 1关注 0查看 608

<?php

use Ratchet\MessageComponentInterface;

use Ratchet\ConnectionInterface;

// Make sure composer dependencies have been installed

require __DIR__ . '/vendor/autoload.php';

/**

* chat.php

* Send any incoming messages to all connected clients (except sender)

*/

class MyChat implements MessageComponentInterface {

protected $clients;

public function __construct() {

$this->clients = new \SplObjectStorage;

}

public function onOpen(ConnectionInterface $conn) {

$this->clients->attach($conn);

}

public function onMessage(ConnectionInterface $from, $msg) {

foreach ($this->clients as $client) {

if ($from != $client) {

$client->send($msg);

}

}

}

public function onClose(ConnectionInterface $conn) {

$this->clients->detach($conn);

}

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

$conn->close();

}

}

// Run the server application through the WebSocket protocol on port 8080

$app = new Ratchet\App('localhost', 8080);

$app->route('/chat', new MyChat);

$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));

$app->run();

上面是服务端的代码,怎么接收客户端传来的内容,并发送内容至客户端?谢谢!!!

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档