本文整理汇总了PHP中Ratchet\ConnectionInterface::send方法的典型用法代码示例。如果您正苦于以下问题:PHP ConnectionInterface::send方法的具体用法?PHP ConnectionInterface::send怎么用?PHP ConnectionInterface::send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ratchet\ConnectionInterface
的用法示例。
在下文中一共展示了ConnectionInterface::send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onOpen
public function onOpen(ConnectionInterface $from, RequestInterface $request = null)
{
echo "New HTTP connection!\n";
//Variables in URLs are not supported in Ratchet for now
//See https://github.com/cboden/Ratchet/pull/143
$requestPath = $request->getPath();
$pathParts = explode('/', preg_replace('#^/peerjs/#', '', $requestPath));
//Remove /peerjs
$action = array_pop($pathParts);
$peerToken = array_pop($pathParts);
$peerId = array_pop($pathParts);
$key = array_pop($pathParts);
$respStatus = 200;
$respHeaders = array('X-Powered-By' => \Ratchet\VERSION, 'Access-Control-Allow-Origin' => '*');
$respBody = null;
switch ($action) {
case 'id':
$respHeaders['Content-Type'] = 'text/html';
if ($peerId === null) {
do {
$peerId = substr(sha1(uniqid('', true) . mt_rand()), 0, self::PEERID_LENGTH);
} while ($this->peerServer->peerIdExists($peerId));
}
$respBody = $peerId;
break;
case 'peers':
if (self::ALLOW_DISCOVERY) {
$peers = $this->peerServer->listPeers();
$list = array();
foreach ($peers as $peer) {
$list[] = $peer['id'];
}
$respBody = $list;
} else {
$respStatus = 401;
// Access denied
}
break;
case 'offer':
case 'candidate':
case 'answer':
case 'leave':
//TODO: start streaming?
//TODO: start streaming?
default:
$respStatus = 400;
//Bad request
}
if (is_array($respBody)) {
// Encode to JSON
$respHeaders['Content-Type'] = 'application/json';
$respBody = json_encode($respBody);
}
//Send response
$response = new Response($respStatus, $respHeaders, (string) $respBody);
$from->send((string) $response);
$from->close();
}
示例2: onOpen
/**
* @param \Ratchet\ConnectionInterface $conn
* @param \Guzzle\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!!
* @throws \UnexpectedValueException if a RequestInterface is not passed
*/
public function onOpen(ConnectionInterface $conn, \Guzzle\Http\Message\RequestInterface $request = null)
{
$response = new \Guzzle\Http\Message\Response(200);
$response->setBody(file_get_contents($this->path));
$conn->send($response);
$conn->close();
}
示例3: onMessage
/**
* Triggered when a client sends data through the socket
* @param \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
* @param string $msg The message received
* @throws \Exception
*/
function onMessage(ConnectionInterface $from, $msg)
{
echo $_SESSION["city"];
if ($msg == "update") {
$from->send(updateMessage());
} else {
parse_request($msg);
}
}
示例4: onOpen
final function onOpen(ConnectionInterface $conn, GuzzleRequest $request = null)
{
$this->onRequest($request);
$userData = $this->session->get('user');
if (count($userData) == 2 && is_int($userData[0])) {
$userId = $userData[0];
$userRole = $userData[1];
$this->securityProvider->setRole($userData[1]);
if ($this->securityProvider->isGranted($this->expectedRole)) {
$actionRequest = new Request($request, $userId);
$response = call_user_func($this->callback, $actionRequest);
} else {
$response = new Response('ACCESS DENIED', Response::HTTP_FORBIDDEN);
}
$conn->send((string) $response);
}
$conn->close();
}
示例5: onMessage
public function onMessage(ConnectionInterface $from, $msg)
{
$from->send($msg);
$now = date(DATE_RFC2822);
$headers = $from->WebSocket->request->getHeader('User-Agent');
echo "{$now} -- id:{$from->resourceId} -- {$from->remoteAddress} -- {$headers} -- ({$msg})\n";
}
示例6: serve
public function serve(ConnectionInterface $conn, RequestInterface $request = null, array $parameters)
{
try {
$application = $this->applications->get($parameters['application']);
$conn->send((string) $application->execute($parameters['module'], $request));
$conn->close();
} catch (ApplicationNotFoundException $e) {
$response = new Response(404, null, $e->getMessage());
$conn->send((string) $response);
$conn->close();
} catch (\Exception $e) {
$response = new Response(500, null, $e->getMessage());
$conn->send((string) $response);
$conn->close();
}
}
示例7: onMessage
function onMessage(ConnectionInterface $from, $msg)
{
$msg = explode(' ', $msg);
$msg[0] = 'pong';
$from->send(implode(' ', $msg));
$from->close();
}
示例8: onMessage
public function onMessage(ConnectionInterface $from, $msg)
{
if ($this->mpd->checkConnection()) {
$this->mpdState = true;
} else {
$this->mpdState = false;
$this->mpdConnect();
}
if ($this->mpdState) {
$msg = json_decode($msg);
switch ($msg->type) {
case 'echo':
$from->send(json_encode(['value' => $msg->value, 'callback' => $msg->callback]));
break;
case 'mpdCommand':
if ($this->mpdState) {
$value = $this->mpd->writeCommand($msg->command);
$from->send(json_encode(['value' => $value, 'callback' => $msg->callback]));
} else {
$from->send(json_encode(['value' => '', 'callback' => $msg->callback]));
}
break;
}
}
}
示例9: onMessage
public function onMessage(ConnectionInterface $from, $msg)
{
$datas = json_decode($msg, true);
/**
* We must check if the token is sent
* If there is no token, we cant log the player
* and link him to the current game.
*/
if (!isset($datas['type'])) {
$from->send('Invalid or inaccessible type');
return;
}
if ($datas['']) {
} else {
if ($datas['']) {
}
}
echo "Recieving new message : {$msg} from {$from->resourceId}.\n";
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send(json_encode($datas, true));
}
}
}
示例10: onMessage
public function onMessage(ConnectionInterface $from, $msg)
{
//echo("[" . date('m/d/Y H:i:s') . "] $msg\r\n");
$returns = handleControllerData($msg);
$id = count($returns) - 2;
$stopSame = $returns[$id];
unset($returns[$id]);
if ($returns != NULL && $returns != "") {
foreach ($this->clients as $client) {
if (!$stopSame) {
$client->send(json_encode($returns, JSON_FORCE_OBJECT));
} else {
if ($stopSame == 1) {
if ($from !== $client) {
$client->send(json_encode($returns, JSON_FORCE_OBJECT));
}
} else {
if ($stopSame == 2) {
// special case to send back variable from the recipient
$from->send(json_encode($returns, JSON_FORCE_OBJECT));
}
}
}
}
}
}
示例11: eventconnect
private function eventconnect(ConnectionInterface $from, $data)
{
$avatar = 'http://api.adorable.io/avatars/150/' . rand(100000, 999999) . '.png';
$this->clients[$from->resourceId]['avatar'] = $avatar;
$this->clients[$from->resourceId]['is_admin'] = $data->is_admin;
$send_data = ['event' => 'connect', 'clients' => $this->clients];
$from->send(json_encode(['event' => 'connected', 'avatar' => $avatar]));
$this->sendMessageToAll($send_data);
}
示例12: onMessage
public function onMessage(ConnectionInterface $from, $msg)
{
$message = json_decode($msg);
if (isset($message)) {
echo $message->command . "(" . $from->resourceId . ") > ";
if ($message->command == "read") {
echo "reading data ... \n";
$from->testObject->success = true;
$from->send(json_encode($from->testObject));
}
if ($message->command == "create") {
echo "set data ...\n";
$from->testObject = $message->data;
// someting like update in db
//$globalTestObject = $message->data;
$from->testObject->success = true;
$from->send(json_encode($message->data));
}
}
/* its send to all clients recived message
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
*/
}
示例13: onOpen
public function onOpen(ConnectionInterface $from, RequestInterface $request = null)
{
if (empty($request)) {
$resp = new Response(400);
$from->send((string) $resp);
$from->close();
return;
}
// Session management
$saveHandler = $this->_handler;
$id = $request->getCookie(ini_get('session.name'));
if (empty($id)) {
$id = sha1(uniqid() . mt_rand());
}
// Crappy workaround for sessions - don't know why they are not saved
// @see https://github.com/ratchetphp/Ratchet/blob/master/src/Ratchet/Session/SessionProvider.php
if (isset($this->sessions[$id])) {
$from->Session = $this->sessions[$id];
} else {
$from->Session = new Session(new VirtualSessionStorage($saveHandler, $id, $this->_serializer));
$this->sessions[$id] = $from->Session;
}
if (ini_get('session.auto_start')) {
$from->Session->start();
}
$this->onRequest($from, $request);
}
示例14: onOpen
public function onOpen(ConnectionInterface $conn)
{
$username = $conn->WebSocket->request->getQuery()->get('username');
// Store the new connection to send messages to later
$this->clients[$conn->resourceId] = $conn;
$conn->send(json_encode(['type' => 'welcome']));
echo "New connection! ({$conn->resourceId})\n";
$this->updateClientLists();
}
示例15: onMessage
public function onMessage(ConnectionInterface $from, $json)
{
$data = json_decode($json);
if (!isset($data->id) || !isset($data->action)) {
$from->send(json_encode(array('status' => 'error', 'message' => 'Invalid values.')));
return;
}
switch ($data->action) {
case "join":
foreach ($this->clients as $client) {
if ($from == $client) {
$from->send(json_encode(array('status' => 'error', 'message' => 'You already joined to any.')));
return;
}
}
if (!isset($this->rooms[$data->id])) {
$this->rooms[$data->id] = new \SplObjectStorage();
}
$this->rooms[$data->id]->attach($from);
$this->clients->attach($from);
$from->send(json_encode(array('status' => 'success')));
break;
case "post":
foreach ($this->rooms as $id => $storage) {
if ($storage->contains($from) && $id === $data->id) {
foreach ($storage as $client) {
if ($from != $client) {
$client->send($json);
}
}
$from->send(json_encode(array('status' => 'success')));
break;
}
}
$from->send(json_encode(array('status' => 'error', 'message' => 'Invalid values.')));
break;
default:
$from->send(json_encode(array('status' => 'error', 'message' => 'Invalid action.')));
break;
}
}