Skip to content

Commit 2af7c38

Browse files
committed
Allow explicit configuration on included transports
1 parent 573e5e6 commit 2af7c38

23 files changed

+598
-569
lines changed

README.md

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ A simplified echo SockJS server could look more or less like:
4747
const http = require('http');
4848
const sockjs = require('sockjs');
4949

50-
const echo = sockjs.createServer({ sockjs_url: 'http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js' });
50+
const echo = sockjs.createServer({ prefix:'/echo', sockjs_url: 'http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js' });
5151
echo.on('connection', function(conn) {
52-
conn.on('data', function(message) {
53-
conn.write(message);
54-
});
55-
conn.on('close', function() {});
52+
conn.on('data', function(message) {
53+
conn.write(message);
54+
});
55+
conn.on('close', function() {});
5656
});
5757

5858
const server = http.createServer();
59-
echo.installHandlers(server, {prefix:'/echo'});
59+
echo.attach(server);
6060
server.listen(9999, '0.0.0.0');
6161
```
6262

@@ -98,7 +98,7 @@ Where `options` is a hash which can contain:
9898
domain local to the SockJS server. This iframe also does need to
9999
load SockJS javascript client library, and this option lets you specify
100100
its url (if you're unsure, point it to
101-
<a href="http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js">
101+
<a href="http://cdn.jsdelivr.net/sockjs/1/sockjs.min.js">
102102
the latest minified SockJS client release</a>, this is the default).
103103
You must explicitly specify this url on the server side for security
104104
reasons - we don't want the possibility of running any foreign
@@ -122,10 +122,10 @@ Where `options` is a hash which can contain:
122122
streaming and will make streaming transports to behave like polling
123123
transports. The default value is 128K.</dd>
124124

125-
<dt>websocket (boolean)</dt>
126-
<dd>Some load balancers don't support websockets. This option can be used
127-
to disable websockets support by the server. By default websockets are
128-
enabled.</dd>
125+
<dt>transports (Array of strings)</dt>
126+
<dd>List of transports to enable. Select from `eventsource`, `htmlfile`,
127+
`jsonp-polling`, `websocket`, `websocket-raw`, `xhr-polling`,
128+
and `xhr-streaming`.</dd>
129129

130130
<dt>jsessionid (boolean or function)</dt>
131131
<dd>Some hosting providers enable sticky sessions only to requests that
@@ -137,7 +137,7 @@ Where `options` is a hash which can contain:
137137
<dt>log (function(severity, message))</dt>
138138
<dd>It's quite useful, especially for debugging, to see some messages
139139
printed by a SockJS-node library. This is done using this `log`
140-
function, which is by default set to `console.log`. If this
140+
function, which is by default set to nothing. If this
141141
behaviour annoys you for some reason, override `log` setting with a
142142
custom handler. The following `severities` are used: `debug`
143143
(miscellaneous logs), `info` (requests logs), `error` (serious
@@ -161,7 +161,7 @@ Where `options` is a hash which can contain:
161161
<a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing">CORS</a>
162162
headers from being included in the HTTP response. Can be used when the
163163
sockjs client is known to be connecting from the same origin as the
164-
sockjs server.</dd>
164+
sockjs server. This also disables the iframe HTML endpoint.</dd>
165165
</dl>
166166

167167

@@ -172,13 +172,10 @@ Once you have create `Server` instance you can hook it to the
172172

173173
```javascript
174174
var http_server = http.createServer();
175-
sockjs_server.installHandlers(http_server, options);
175+
sockjs_server.attach(http_server);
176176
http_server.listen(...);
177177
```
178178

179-
Where `options` can overshadow options given when creating `Server`
180-
instance.
181-
182179
`Server` instance is an
183180
[EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter),
184181
and emits following event:
@@ -191,7 +188,7 @@ and emits following event:
191188
All http requests that don't go under the path selected by `prefix`
192189
will remain unanswered and will be passed to previously registered
193190
handlers. You must install your custom http handlers before calling
194-
`installHandlers`.
191+
`attach`.
195192

196193
### Connection instance
197194

@@ -266,14 +263,13 @@ For example:
266263

267264
```javascript
268265
sockjs_server.on('connection', function(conn) {
269-
console.log('connection' + conn);
270-
conn.on('close', function() {
271-
console.log('close ' + conn);
272-
});
273-
conn.on('data', function(message) {
274-
console.log('message ' + conn,
275-
message);
276-
});
266+
console.log('connection' + conn);
267+
conn.on('close', function() {
268+
console.log('close ' + conn);
269+
});
270+
conn.on('data', function(message) {
271+
console.log('message ' + conn, message);
272+
});
277273
});
278274
```
279275

lib/app.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

lib/generic-app.js

Lines changed: 0 additions & 117 deletions
This file was deleted.

lib/handlers.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
module.exports = {
4+
welcome_screen(req, res) {
5+
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
6+
res.writeHead(200);
7+
res.end('Welcome to SockJS!\n');
8+
},
9+
10+
handle_404(req, res) {
11+
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
12+
res.writeHead(404);
13+
res.end('404 Error: Page not found\n');
14+
},
15+
16+
handle_405(req, res, methods) {
17+
res.writeHead(405, { 'Allow': methods.join(', ') });
18+
res.end();
19+
},
20+
21+
handle_error(err, req, res) {
22+
if (res.finished) {
23+
return;
24+
}
25+
if (typeof err === 'object' && 'status' in err) {
26+
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
27+
res.writeHead(err.status);
28+
res.end(err.message || '');
29+
} else {
30+
try {
31+
res.writeHead(500);
32+
res.end('500 - Internal Server Error');
33+
} catch (ex) {
34+
this.options.log('error', `Exception on "${req.method} ${req.url}" in filter "${req.last_fun}":\n${ex.stack || ex}`);
35+
}
36+
}
37+
},
38+
};

lib/transport/iframe.js renamed to lib/iframe.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const utils = require('../utils');
4-
const middleware = require('../middleware');
3+
const utils = require('./utils');
4+
const middleware = require('./middleware');
55

66
const iframe_template = `<!DOCTYPE html>
77
<html>

lib/info.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const middleware = require('./middleware');
66
module.exports = {
77
info(req, res) {
88
const info = {
9-
websocket: this.options.websocket,
9+
// deprecated option, but useful for old clients
10+
websocket: this.options.transports.includes('websocket'),
11+
transports: this.options.transports,
1012
origins: this.options.disable_cors ? undefined : ['*:*'],
1113
cookie_needed: !!this.options.jsessionid,
1214
entropy: utils.random32(),

0 commit comments

Comments
 (0)