Comparing version 2.0.3 to 2.0.4
const Ws = require("ws"); | ||
const sessionof = (url, splitter) => { | ||
const headof = (url, splitter) => { | ||
if (url.startsWith("http://")) { | ||
@@ -41,7 +41,18 @@ url = url.substring(url.indexOf("/", 7)); | ||
const dispatch = (receptor, head, body, response) => { | ||
if (head[0] === "-") { | ||
receptor.onmessage(head.substring(1), body); | ||
response.end(); | ||
} else if (head[0] === "~") { | ||
receptor.onrequest(head.substring(1), body, (body) => { response.end(body) }); | ||
} else { | ||
throw new Error("Request head should start with either '-' or '~', got: "+head); | ||
} | ||
}; | ||
function onrequest (request, response) { | ||
let session = sessionof(request.url, this._antena_splitter); | ||
if (session) { | ||
let head = headof(request.url, this._antena_splitter); | ||
if (head) { | ||
if (request.headers["content-length"] === 0) { | ||
this._antena_receptor.onrequest(session, "", (body) => { response.end(body) }); | ||
dispatch(this._antena_receptor, head, body, response); | ||
} else { | ||
@@ -51,3 +62,3 @@ let body = ""; | ||
request.on("end", () => { | ||
this._antena_receptor.onrequest(session, body, (body) => { response.end(body) }); | ||
dispatch(this._antena_receptor, head, body, response); | ||
}); | ||
@@ -61,3 +72,3 @@ } | ||
function onupgrade (request, socket, head) { | ||
let session = sessionof(request.url, this._antena_splitter); | ||
let session = headof(request.url, this._antena_splitter); | ||
if (session) { | ||
@@ -64,0 +75,0 @@ this._antena_ws_server.handleUpgrade(request, socket, head, (websocket) => { |
@@ -10,5 +10,8 @@ | ||
const client = { | ||
_request_url: "http"+secure+"://"+hostname+port+"/"+splitter+"/"+session, | ||
_request_url: "http"+secure+"://"+hostname+port+"/"+splitter+"/~"+session, | ||
_connect_url: "ws"+secure+"://"+hostname+port+"/"+splitter+"/"+session, | ||
_websocket: [], | ||
_websocket: { | ||
_url: "http"+secure+"://"+hostname+port+"/"+splitter+"/-"+session, | ||
send: fallback_send | ||
}, | ||
onmessage: null, | ||
@@ -19,3 +22,2 @@ session, | ||
}; | ||
client._websocket.send = Array.prototype.push; | ||
const websocket = new WebSocket(client._connect_url); | ||
@@ -33,7 +35,11 @@ websocket._antena_client = client; | ||
function onopen (event) { | ||
for (let index = 0; index < this._antena_client._websocket.length; index++) | ||
this.send(this._antena_client._websocket[index]); | ||
this._antena_client._websocket = this; | ||
} | ||
function fallback_send (message) { | ||
const request = new XMLHttpRequest(); | ||
request.open("PUT", this._url); | ||
request.send(message); | ||
} | ||
function send (message) { | ||
@@ -40,0 +46,0 @@ this._websocket.send(message); |
{ | ||
"name": "antena", | ||
"description": "Isomorphic communication library", | ||
"version": "2.0.3", | ||
"description": "Isomorphic communication library for node and browsers", | ||
"version": "2.0.4", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Laurent Christophe", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18972
446