-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeteor.module
115 lines (88 loc) · 2.61 KB
/
meteor.module
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
<?php
function meteor_menu() {
$items['meteor-test'] = array(
'title' => 'Meteor Test Page',
'page callback' => 'meteor_test_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function meteor_test_page() {
$output = <<<EOF
<div id="test">
Hello World<br>
</div>
EOF;
meteor_client_join_channel('demo', 'function(data) { $("#test").append(data + "<br><br>"); }');
return $output;
}
function meteor_demo_channel($message) {
$mp = meteor_open_channel('demo');
meteor_send_message($mp, $message);
meteor_close_channel($mp, $message);
}
function meteor_add_js() {
static $added = FALSE;
if ($added === TRUE) {
return;
}
$settings = array();
$hostid = '';
if (isset($_SESSION['meteor_hostid'])) {
$hostid = $_SESSION['meteor_hostid'];
}
else {
$hostid = user_password();
$_SESSION['meteor_hostid'] = $hostid; // Note: This is allowed here as we only support non-cached pages for now
}
$settings['hostid'] = $hostid;
$settings['host'] = variable_get('meteor_host', NULL);
$settings['mode'] = variable_get('meteor_mode', 'stream');
$settings['debug'] = variable_get('meteor_debug', FALSE);
drupal_set_html_head('<script type="text/javascript" src="http://' . $settings['host']. '/meteor.js"></script>');
drupal_add_js(array('meteor' => $settings), 'setting');
drupal_add_js(drupal_get_path('module', 'meteor') . '/js/meteor_client.js');
$added = TRUE;
}
function meteor_client_join_channel($channelid, $callback, $backtrack = FALSE, $mode = 'stream') {
meteor_add_js();
$channels = array();
$channels[] = array (
'channelid' => $channelid,
'backtrack' => $backtrack,
'callback' => $callback
);
drupal_add_js(array('meteor' => array('channels' => $channels)), 'setting');
}
function meteor_open_channel($channelid) {
if (!($fp = fsockopen("127.0.0.1", 4671, $errno, $errstr, 5))) {
// echo "Meteor not responding\n";
// FIXME: Add watchdog
return NULL;
}
$mp = (object) array(
'channelid' => $channelid,
'fp' => $fp
);
return $mp;
}
function meteor_send_message($mp, $message) {
$msg = array(
'data' => $message,
'channelid' => $mp->channelid
);
$json_message = rawurlencode(drupal_to_js($msg));
$out = "ADDMESSAGE ".$mp->channelid." ".$json_message."\n";
$written = fwrite($mp->fp, $out);
usleep(10000);
$buf = fread($mp->fp, 4096);
if (substr($buf, 2, 2) != 'OK') {
return FALSE;
}
return TRUE;
}
function meteor_close_channel($mp, $close_channel = FALSE) {
// TODO: Send CLOSECHANNEL <x> message on close_channel == TRUE
fclose($mp->fp);
}