-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
xhr-streaming.js
64 lines (57 loc) · 1.64 KB
/
xhr-streaming.js
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
'use strict';
const ResponseReceiver = require('./response-receiver');
const Session = require('../session');
const middleware = require('../middleware');
const xhr = require('./xhr');
class XhrStreamingReceiver extends ResponseReceiver {
constructor(req, res, options) {
super(req, res, options);
this.protocol = 'xhr-streaming';
}
sendFrame(payload) {
return super.sendFrame(payload + '\n');
}
}
function xhr_streaming(req, res, _head, next) {
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
res.writeHead(200);
// IE requires 2KB prefix:
// http://blogs.msdn.com/b/ieinternals/archive/2010/04/06/comet-streaming-in-internet-explorer-with-xmlhttprequest-and-xdomainrequest.aspx
res.write(Array(2049).join('h') + '\n');
Session.register(req, this, new XhrStreamingReceiver(req, res, this.options));
next();
}
module.exports = {
routes: [
{
method: 'POST',
path: '/xhr_streaming',
handlers: [middleware.h_sid, middleware.h_no_cache, middleware.xhr_cors, xhr_streaming],
transport: true
},
{
method: 'OPTIONS',
path: '/xhr_streaming',
handlers: [middleware.h_sid, middleware.xhr_cors, middleware.xhr_options],
transport: true
},
{
method: 'POST',
path: '/xhr_send',
handlers: [
middleware.h_sid,
middleware.h_no_cache,
middleware.xhr_cors,
middleware.expect_xhr,
xhr.xhr_send
],
transport: true
},
{
method: 'OPTIONS',
path: '/xhr_send',
handlers: [middleware.h_sid, middleware.xhr_cors, middleware.xhr_options],
transport: true
}
]
};