Skip to content

Commit

Permalink
Add the version protocol header
Browse files Browse the repository at this point in the history
* A workaround for URI in WebSocket transport seems to conflict with the current version of `ws` package.

Fixes #19
  • Loading branch information
flowersinthesand committed May 3, 2017
1 parent f2a9df1 commit f83e99c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
3 changes: 3 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ function createSocket(options) {
// sends it as the first message of transport. A client socket will fire `open` event.
transport.send(url.format({
query: {
// This value should be calculated through protocol version the client implemented.
// However, now that `1.0` is the only version, we don't need to do that.
"cettia-version": "1.0",
"cettia-id": id,
"cettia-heartbeat": options.heartbeat,
"cettia-_heartbeat": options._heartbeat
Expand Down
2 changes: 2 additions & 0 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ module.exports = function(uri, options) {
if (id) {
urlObj.query["cettia-id"] = id;
}
// Add the implemented protocol version.
urlObj.query["cettia-version"] = "1.0";
// URI whose scheme is `http` or `https` and `cettia-transport-name` param doesn't exist is
// called an abbreviated one. It should be translated into three separate URIs corresponding
// to WebSocket, HTTP streaming and HTTP long polling respectively. For example, if uri is
Expand Down
12 changes: 7 additions & 5 deletions lib/transport-http-longpoll-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ module.exports = function(uri, options) {
// The current holding request.
var req;
self.connect = function() {
var reqOpts = url.parse(uri + "&cettia-transport-when=open");
// The following transport protocol headers should be added in the first request:
// * `cettia-transport-when`: `open`.
// * `cettia-transport-version`: the implemented long polling transport version.
// * `cettia-transport-jsonp`: `true` required in case of JSONP.
// * `cettia-transport-callback`: callback name required in case of JSONP.
var reqOpts = url.parse(uri + "&cettia-transport-version=1.0&cettia-transport-when=open");
// Somehow it's turned out that KeepAlive Agent is required on Node 4+.
reqOpts.agent = new http.Agent({keepAlive: true});
// Performs a HTTP persistent connection through `GET` method. The first request's
// `cettia-transport-when` param should be `open`. In case of JSONP,
// `cettia-transport-jsonp` param should be `true` and `cettia-transport-callback`
// param should be provided as well.
// Performs a HTTP persistent connection through `GET` method.
req = http.request(reqOpts)
.on("error", onerror).on("close", onclose)
.on("response", function(res) {
Expand Down
34 changes: 24 additions & 10 deletions lib/transport-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,18 @@ function createStreamTransport(req, res) {
// change of the response. It should be greater than 1KB, be composed of white space character
// and end with `\n`.
var text2KB = Array(2048).join(" ");
// Some host objects which are used to perform streaming transport can't or don't allow to
// read response headers as well as write request headers. That's why we uses the first
// message as a handshake output. The handshake result should be formatted in URI. And
// transport id should be added as `cettia-transport-id` param.
var uri = url.format({query: {"cettia-transport-id": self.id}});
// Some host objects which are used to perform streaming transport can't or don't allow to read
// HTTP response headers as well as write HTTP request headers. That's why we uses the first
// message as a handshake output. The handshake result should be formatted in URI.
var uri = url.format({
query: {
// Adds the following transport protocol headers:
// * `cettia-transport-id`: an identifier.
// * `cettia-transport-version`: the implemented streaming transport version.
"cettia-transport-version": "1.0",
"cettia-transport-id": self.id
}
});
// Likewise some host objects in old browsers, junk data is needed to make themselves be aware
// of change of response. Prints the padding and the first message following
// `text/event-stream` with `utf-8` encoding.
Expand Down Expand Up @@ -343,11 +350,18 @@ function createLongpollTransport(req, res) {
// The first request
case "open":
// `script` tag which is a host object used in old browsers to perform long polling
// transport can't read response headers as well as write request headers. That's why we
// uses the first response's body as a handshake output instead of headers. The
// handshake result should be formatted in URI. And transport id should be added as
// `cettia-transport-id` param.
res.endWithMessage(url.format({query: {"cettia-transport-id": self.id}}));
// transport can't read HTTP response headers as well as write HTTP request headers. That's
// why we uses the first response's body as a handshake output instead of HTTP headers. The
// handshake result should be formatted in URI.
res.endWithMessage(url.format({
// Adds the following transport protocol headers:
// * `cettia-transport-id`: an identifier.
// * `cettia-transport-version`: the implemented long polling transport version.
query: {
"cettia-transport-version": "1.0",
"cettia-transport-id": self.id
}
}));
break;
// The succeeding request after the first request
case "poll":
Expand Down
10 changes: 6 additions & 4 deletions lib/transport-http-stream-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ module.exports = function(uri, options) {
self.connect = function() {
// `close` event might be called twice
var closed;
// Performs a HTTP persistent connection through `GET` method. `cettia-transport-when`
// param should be `open`. In case of Server-Sent Events, `cettia-transport-sse` param
// should be `true`.
var reqOpts = url.parse(uri + "&cettia-transport-when=open");
// Performs a HTTP persistent connection through `GET` method. Adds the following transport
// protocol headers:
// * `cettia-transport-when`: `open`.
// * `cettia-transport-version`: the implemented streaming transport version.
// * `cettia-transport-sse`: `true` required in case of Server-Sent Events.
var reqOpts = url.parse(uri + "&cettia-transport-version=1.0&cettia-transport-when=open");
// Somehow it's turned out that KeepAlive Agent is required on Node 4+.
reqOpts.agent = new http.Agent({keepAlive: true});
req = http.request(reqOpts)
Expand Down
3 changes: 0 additions & 3 deletions lib/transport-websocket-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ module.exports = function(uri, options) {
var self = createBaseTransport(uri, options);
var ws;
self.connect = function() {
// Adds a trailing slash to authority if query is not empty.
// https://github.com/websockets/ws/issues/258
uri = uri.replace(/([^\/])\?/, "$1\/\?");
// Simply delegates WebSocket's events to transport and transport's behaviors to WebSocket.
ws = new WebSocket(uri);
ws.onopen = function() {
Expand Down

0 comments on commit f83e99c

Please sign in to comment.