This repository was archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheventstream.js
More file actions
194 lines (178 loc) · 3.58 KB
/
eventstream.js
File metadata and controls
194 lines (178 loc) · 3.58 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
189
190
191
192
193
194
/**
* The event stream constructor.
*
* @access public
* @param object an http(s) request object
* @param object an http(s) response object
*/
module.exports.EventStream = function(req, res) {
var isOpen = true;
req.on('close', function() {
isOpen = false;
});
/**
* Last-Event-ID header value
*/
this.lastEventId = null;
/**
* Initialize the stream
*
* @access public
* @return void
*/
this.init = function() {
this.lastEventId = req.headers['last-event-id'] || null;
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8'
});
};
/**
* Is the connection open?
*/
this.isOpen = function() {
return isOpen;
};
/**
* Send a message to the client
*
* @access public
* @param object the options object
* @return void
*/
this.send = function(opts) {
opts = opts || { };
opts.value = opts.value || '';
opts.field = (typeof opts.field === 'string')
? opts.field
: 'data';
opts.value = (typeof opts.encode === 'function')
? opts.encode(opts.value)
: this.encode(opts.value);
opts.value.split("\n").forEach(function(line) {
res.write(opts.field + ': ' + line + '\n');
});
if(opts.field == 'data') {
res.write('\n');
}
};
/**
* Close the stream
*
* @access public
* @return void
*/
this.close = function() {
isOpen = false;
res.end();
};
/**
* Send a keep-alive message
*/
this.keepAlive = function() {
this.sendComment('Keep-Alive');
};
};
/**
* The data encoding method. This can be overriden to allow easy sending
* of serialized data (eg. JSON).
*
* @access public
* @param string
*/
module.exports.EventStream.prototype.encode = function(value) {
return String(value);
};
/**
* Send a comment to the client
*
* @access public
* @param string the comment
* @return void
*/
module.exports.EventStream.prototype.sendComment = function(text) {
return this.send({
field: '',
value: text,
encode: String
});
};
/**
* Send an "event" message to the client
*
* @access public
* @param string the value to send
* @return void
*/
module.exports.EventStream.prototype.sendEvent = function(event) {
return this.send({
field: 'event',
value: event,
encode: String
});
};
/**
* Send a "data" message to the client
*
* @access public
* @param mixed the value to send
* @return void
*/
module.exports.EventStream.prototype.sendData = function(data) {
return this.send({
field: 'data',
value: data
});
};
/**
* Send an "id" message to the client
*
* @access public
* @param string the value to send
* @return void
*/
module.exports.EventStream.prototype.sendId = function(id) {
this.lastEventId = String(id);
return this.send({
field: 'id',
value: id,
encode: String
});
};
/**
* Send a "retry" message to the client
*
* @access public
* @param number the number of milliseconds to wait until retrying the connection
* @return void
*/
module.exports.EventStream.prototype.sendRetry = function(millisecs) {
millisecs = parseInt(millisecs);
if (isNaN(millisecs)) {
throw new TypeError();
}
return this.send({
field: 'retry',
value: millisecs,
encode: String
});
};
/**
* Send a message, with other optional params
*
* @access public
* @param object the options object
* @return void
*/
module.exports.EventStream.prototype.sendMessage = function(opts) {
if (opts.event) {
this.sendEvent(opts.event);
}
if (opts.id) {
this.sendId(opts.id);
}
if (opts.retry) {
this.sendRetry(opts.retry);
}
this.sendData(opts.data);
};
/* End of file eventstream.js */