| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
namespace Ratchet\Wamp;
|
|
|
3 |
use Ratchet\ConnectionInterface;
|
|
|
4 |
use Ratchet\AbstractConnectionDecorator;
|
|
|
5 |
use Ratchet\Wamp\ServerProtocol as WAMP;
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* A ConnectionInterface object wrapper that is passed to your WAMP application
|
|
|
9 |
* representing a client. Methods on this Connection are therefore different.
|
|
|
10 |
* @property \stdClass $WAMP
|
|
|
11 |
*/
|
|
|
12 |
class WampConnection extends AbstractConnectionDecorator {
|
|
|
13 |
/**
|
|
|
14 |
* {@inheritdoc}
|
|
|
15 |
*/
|
|
|
16 |
public function __construct(ConnectionInterface $conn) {
|
|
|
17 |
parent::__construct($conn);
|
|
|
18 |
|
|
|
19 |
$this->WAMP = new \StdClass;
|
|
|
20 |
$this->WAMP->sessionId = str_replace('.', '', uniqid(mt_rand(), true));
|
|
|
21 |
$this->WAMP->prefixes = array();
|
|
|
22 |
|
|
|
23 |
$this->send(json_encode(array(WAMP::MSG_WELCOME, $this->WAMP->sessionId, 1, \Ratchet\VERSION)));
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Successfully respond to a call made by the client
|
|
|
28 |
* @param string $id The unique ID given by the client to respond to
|
|
|
29 |
* @param array $data an object or array
|
|
|
30 |
* @return WampConnection
|
|
|
31 |
*/
|
|
|
32 |
public function callResult($id, $data = array()) {
|
|
|
33 |
return $this->send(json_encode(array(WAMP::MSG_CALL_RESULT, $id, $data)));
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Respond with an error to a client call
|
|
|
38 |
* @param string $id The unique ID given by the client to respond to
|
|
|
39 |
* @param string $errorUri The URI given to identify the specific error
|
|
|
40 |
* @param string $desc A developer-oriented description of the error
|
|
|
41 |
* @param string $details An optional human readable detail message to send back
|
|
|
42 |
* @return WampConnection
|
|
|
43 |
*/
|
|
|
44 |
public function callError($id, $errorUri, $desc = '', $details = null) {
|
|
|
45 |
if ($errorUri instanceof Topic) {
|
|
|
46 |
$errorUri = (string)$errorUri;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$data = array(WAMP::MSG_CALL_ERROR, $id, $errorUri, $desc);
|
|
|
50 |
|
|
|
51 |
if (null !== $details) {
|
|
|
52 |
$data[] = $details;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
return $this->send(json_encode($data));
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* @param string $topic The topic to broadcast to
|
|
|
60 |
* @param mixed $msg Data to send with the event. Anything that is json'able
|
|
|
61 |
* @return WampConnection
|
|
|
62 |
*/
|
|
|
63 |
public function event($topic, $msg) {
|
|
|
64 |
return $this->send(json_encode(array(WAMP::MSG_EVENT, (string)$topic, $msg)));
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* @param string $curie
|
|
|
69 |
* @param string $uri
|
|
|
70 |
* @return WampConnection
|
|
|
71 |
*/
|
|
|
72 |
public function prefix($curie, $uri) {
|
|
|
73 |
$this->WAMP->prefixes[$curie] = (string)$uri;
|
|
|
74 |
|
|
|
75 |
return $this->send(json_encode(array(WAMP::MSG_PREFIX, $curie, (string)$uri)));
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Get the full request URI from the connection object if a prefix has been established for it
|
|
|
80 |
* @param string $uri
|
|
|
81 |
* @return string
|
|
|
82 |
*/
|
|
|
83 |
public function getUri($uri) {
|
|
|
84 |
$curieSeperator = ':';
|
|
|
85 |
|
|
|
86 |
if (preg_match('/http(s*)\:\/\//', $uri) == false) {
|
|
|
87 |
if (strpos($uri, $curieSeperator) !== false) {
|
|
|
88 |
list($prefix, $action) = explode($curieSeperator, $uri);
|
|
|
89 |
|
|
|
90 |
if(isset($this->WAMP->prefixes[$prefix]) === true){
|
|
|
91 |
return $this->WAMP->prefixes[$prefix] . '#' . $action;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
return $uri;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* @internal
|
|
|
101 |
*/
|
|
|
102 |
public function send($data) {
|
|
|
103 |
$this->getConnection()->send($data);
|
|
|
104 |
|
|
|
105 |
return $this;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* {@inheritdoc}
|
|
|
110 |
*/
|
|
|
111 |
public function close($opt = null) {
|
|
|
112 |
$this->getConnection()->close($opt);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
}
|