-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWampPublisher.php
More file actions
116 lines (106 loc) · 2.94 KB
/
WampPublisher.php
File metadata and controls
116 lines (106 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* This file is part of bdk/wamp-publisher
*
* @package bdk\PubSub
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2022 Brad Kent
* @version v1.1
* @link http://www.github.com/bkdotcom/WampPublisher
*/
namespace bdk;
use WebSocket\Client;
/**
* Publish messages to a WAMP router
*/
class WampPublisher
{
const CODE_HELLO = 1;
const CODE_PUBLISH = 16;
public $connected = false;
protected $cfg;
protected $client;
/**
* Constructor
*
* @param array $cfg config
* @param array $clientOptions Textalk options
*/
public function __construct($cfg = array(), $clientOptions = array())
{
$this->cfg = \array_merge(array(
'clientOptions' => \array_merge(array(
'headers' => array(
'origin' => 'localhost',
'Sec-WebSocket-Protocol' => 'wamp.2.json',
),
), $clientOptions),
'realm' => 'myRealm',
'url' => 'ws://127.0.0.1:9090/',
), $cfg);
$this->initClient();
}
/**
* Initialize WAMP client
*
* @return void
*/
public function initClient()
{
try {
$this->client = new Client($this->cfg['url'], $this->cfg['clientOptions']);
/*
Perform WAMP handshake
*/
$msg = array(self::CODE_HELLO, $this->cfg['realm'], array());
$this->client->send(\json_encode($msg));
$this->client->receive();
$this->connected = true;
} catch (\Exception $e) {
$this->client = null;
}
}
/**
* Publish to topic
*
* @param string $topic topic
* @param array $args arguments
* @param array $options options
*
* @return void
*/
public function publish($topic, $args = array(), $options = array())
{
if (!$this->client) {
return;
}
$msg = array(
self::CODE_PUBLISH,
$this->getUniqueId(),
$options,
$topic,
$args,
);
$json = \json_encode($msg); // JSON_INVALID_UTF8_SUBSTITUTE
if ($json === false) {
\trigger_error(\json_last_error() . ': ' . \json_last_error_msg());
}
// remove \u0000 from the beginning of any obj keys
// avoid JSON_ERROR_INVALID_PROPERTY_NAME when decoding
$json = \preg_replace('/"\\\\u0000([^"]*)":/', '"$1":', $json);
$this->client->send($json);
}
/**
* Generate a unique id
*
* @return mixed
*/
private function getUniqueId()
{
$filter = 0x1fffffffffffff; // 53 bits
$randomBytes = \openssl_random_pseudo_bytes(8);
list($high, $low) = \array_values(\unpack('N2', $randomBytes));
return \abs(($high << 32 | $low) & $filter);
}
}