-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart.php
More file actions
188 lines (150 loc) · 4.65 KB
/
Copy pathstart.php
File metadata and controls
188 lines (150 loc) · 4.65 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* elgg-nodejs plugin
* Provide tools for interaction with node.js (socket.io, easyrtc) and elgg
*
* @package elgg-nodejs
*/
elgg_register_event_handler('init', 'system', 'elgg_nodejs');
function elgg_nodejs() {
// Extend js view
elgg_extend_view('js/elgg', 'elgg_nodejs/js/tools');
elgg_extend_view('js/elgg', 'elgg_nodejs/js/js');
// Register external javascript for require.js
elgg_define_js('socket.io-client', array(
'src' => $_SERVER['HTTP_HOST'] . ':7890/socket.io/socket.io'
));
elgg_define_js('easyrtc', array(
'src' => $_SERVER['HTTP_HOST'] . ':7890/easyrtc/easyrtc',
'deps' => array('socket.io')
));
elgg_register_js('handlebars', '/mod/elgg-nodejs/vendors/handlebars-v1.3.0');
elgg_load_js('handlebars');
elgg_register_plugin_hook_handler('to:object', 'entity', 'elgg_nodejs_user_to_object');
}
function elgg_nodejs_user_to_object($hook, $type, $return, $params) {
if ($params['entity'] instanceof ElggUser) {
$return->avatar = array(
'tiny' => $params['entity']->getIconURL('tiny'),
'small' => $params['entity']->getIconURL('small')
);
}
return $return;
}
/**
* Get all connected clients of socket.io
* @return [array] All connected clients
*/
function elgg_nodejs_get_clients() {
$url="http://localhost:7890/api/clients";
// Initiate curl
$ch = curl_init();
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$result = curl_exec($ch);
return json_decode($result, true);
}
/**
* Send data to all clients
* @param [type] $data Data to send
* @return [integer] Number of clients reached
*/
function elgg_nodejs_broadcast($data) {
$url="http://localhost:7890/api/clients";
$query = array(
'data' => $data
);
// Initiate curl
$ch = curl_init();
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Add query
curl_setopt($ch,CURLOPT_POST, count($query));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($query));
// Execute
$result = curl_exec($ch);
return json_decode($result, true);
}
/**
* Send data to specifics clients
* @param [string|array] $guids ElggGUID of users we want to send data
* @param [type] $data Data to send
* @return [integer] Number of clients reached
*/
function elgg_nodejs_post_clients($guids, $data) {
$url="http://localhost:7890/api/clients";
if (!is_array($guids)) $guids = array($guids);
$query = array(
'guids' => $guids,
'data' => $data
);
// Initiate curl
$ch = curl_init();
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Add query
curl_setopt($ch,CURLOPT_POST, count($query));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($query));
// Execute
$result = curl_exec($ch);
return json_decode($result, true);
}
/**
* Get all rooms of socket.io
* @return [array] All rooms
*/
function elgg_nodejs_get_rooms() {
$url="http://localhost:7890/api/rooms";
// Initiate curl
$ch = curl_init();
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$result = curl_exec($ch);
return json_decode($result, true);
}
/**
* Send data to sockets rooms
* @param [string|array] $names Name of the rooms we want to send data
* @param [type] $data Data to send
* @return [integer] Number of rooms reached
*/
function elgg_nodejs_post_rooms($names, $data) {
$url="http://localhost:7890/api/rooms";
if (!is_array($names)) $names = array($names);
$query = array(
'names' => $names,
'data' => $data
);
// Initiate curl
$ch = curl_init();
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Add query
curl_setopt($ch,CURLOPT_POST, count($query));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($query));
// Execute
$result = curl_exec($ch);
return json_decode($result, true);
}