-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealplexorComponent.php
More file actions
141 lines (126 loc) · 4.7 KB
/
RealplexorComponent.php
File metadata and controls
141 lines (126 loc) · 4.7 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* @author Ivan Chelishchev <chelishchev@gmail.com>
*/
Yii::import('application.vendors.Realplexor');
class RealplexorComponent extends CApplicationComponent
{
public $host = '127.0.0.1';
public $port = '10010';
public $namespace = '';
/**
* Указывать с http:// в начале. Необходим для доступа к js
* @var string
*/
public $url = '';
/** @var Realplexor */
protected $_rp = null;
protected $_registerJS = false;
public function init()
{
parent::init();
$this->setConfig();
}
protected function setConfig()
{
if(!$this->host || !$this->port || !$this->url)
{
throw new Exception('Where are host or port or url?');
}
if (is_null($this->_rp))
{
$this->_rp = new Realplexor($this->host, $this->port, $this->namespace);
}
}
/**
* Send data to realplexor.
*
* @param mixed $idsAndCursors Target IDs in form of: array(id1 => cursor1, id2 => cursor2, ...)
* of array(id1, id2, id3, ...). If sending to a single ID,
* you may pass it as a plain string, not array.
* @param mixed $data Data to be sent (any format, e.g. nested arrays are OK).
* @param array $showOnlyForIds Send this message to only those who also listen any of these IDs.
* This parameter may be used to limit the visibility to a closed
* number of cliens: give each client an unique ID and enumerate
* client IDs in $showOnlyForIds to not to send messages to others.
* @throws Dklab_Realplexor_Exception
* @return void
*/
public function send($idsAndCursors, $data, $showOnlyForIds = null)
{
$this->_rp->send($idsAndCursors, $data, $showOnlyForIds);
return $this;
}
/**
* Return list of online IDs (keys) and number of online browsers
* for each ID. (Now "online" means "connected just now", it is
* very approximate; more precision is in TODO.)
*
* @param array $idPrefixes If set, only online IDs with these prefixes are returned.
* @return array List of matched online IDs (keys) and online counters (values).
*/
public function cmdOnlineWithCounters($idPrefixes = null)
{
$this->_rp->cmdOnlineWithCounters($idPrefixes);
return $this;
}
/**
* Return list of online IDs.
*
* @param array $idPrefixes If set, only online IDs with these prefixes are returned.
* @return array List of matched online IDs.
*/
public function cmdOnline($idPrefixes = null)
{
return $this->_rp->cmdOnline($idPrefixes);
}
/**
* Return all Realplexor events (e.g. ID offline/offline changes)
* happened after $fromPos cursor.
*
* @param string $fromPos Start watching from this cursor.
* @param array $idPrefixes Watch only changes of IDs with these prefixes.
* @return array List of array("event" => ..., "cursor" => ..., "id" => ...).
*/
public function cmdWatch($fromPos, $idPrefixes = null)
{
return $this->_rp->cmdWatch($fromPos, $idPrefixes);
}
/**
* Регистрация основных js-скриптов
* @return RealplexorComponent
*/
public function putJS()
{
if($this->_registerJS)
{
return $this;
}
Y::a()->clientScript->registerScriptFile($this->url . '/?identifier=SCRIPT&'. time(), CClientScript::POS_HEAD);
Y::a()->clientScript->registerScript($this->namespace . 'jsinit', '
var realplexor = new Dklab_Realplexor(
"' . $this->url . '/?' . time() . '", // URL of engine
"' . $this->namespace . '" // namespace
);
', CClientScript::POS_HEAD
);
$this->_registerJS = true;
return $this;
}
/**
* Вставка js в представление
* function(data, id, cursor)
* @param $nameChannel на что подписываемся?
* @param $func должно быть указано в функции три параметра (data, id, cursor)
*/
public function listen($nameChannel, $func)
{
$this->putJS();
//function(data, id, cursor)
$func = CJavaScript::encode($func);
Y::a()->clientScript->registerScript($nameChannel . 'rp', '
realplexor.subscribe("' . $nameChannel . '", ' . $func . ');
realplexor.execute();
', CClientScript::POS_END);
}
}