Skip to content

Commit

Permalink
Merge pull request #2408 from dedis/js-ws-timeout
Browse files Browse the repository at this point in the history
Fixes timeout in Websocket
  • Loading branch information
nkcr committed Nov 23, 2020
2 parents a56208a + 6d1f681 commit 707b3ca
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions external/js/cothority/package.json
Expand Up @@ -12,6 +12,7 @@
"types": "index.d.ts",
"scripts": {
"build": "npm run protobuf && tsc",
"watch": "tsc --watch",
"linter": "tslint 'src/{,**/}*.ts' 'spec/{,**/}*.ts' && prettier --check *.html",
"linter:fix": "tslint --fix 'src/{,**/}*.ts' 'spec/{,**/}*.ts' && prettier --write *.html",
"test": "npm run linter && npm run cover",
Expand Down
2 changes: 2 additions & 0 deletions external/js/cothority/spec/helpers/bctest.ts
Expand Up @@ -39,6 +39,8 @@ export class BCTest {
try {
const ws = new RosterWSConnection(roster4, StatusRPC.serviceName);
ws.setParallel(1);
ws.setTimeout(999999);

await ws.send(new StatusRequest(), StatusResponse);
Log.warn("Using already running nodes for test!");
usesDocker = false;
Expand Down
6 changes: 6 additions & 0 deletions external/js/cothority/spec/network/connection.spec.ts
Expand Up @@ -114,6 +114,8 @@ describe("WebSocketAdapter Tests", () => {
});

const conn = new RosterWSConnection(roster, "");
conn.setTimeout(999999);

const reply = await conn.send(roster, Roster);

expect(reply instanceof Roster).toBeTruthy();
Expand All @@ -135,6 +137,7 @@ describe("WebSocketAdapter Tests", () => {
});

const conn = new RosterWSConnection(roster, "");
conn.setTimeout(999999);

await expectAsync(conn.send(roster, Roster)).toBeRejected();
});
Expand Down Expand Up @@ -197,7 +200,10 @@ describe("WebSocketAdapter Tests with sendStream", () => {
it("should send and receive data", async (done) => {
const ret = Buffer.from(Roster.encode(new Roster()).finish());
setFactory(() => new TestWebSocket(ret, null, null));

const conn = new WebSocketConnection("http://example.com", "");
conn.setTimeout(999999);

const msg = new Roster();

conn.sendStream(msg, Roster).subscribe({
Expand Down
2 changes: 2 additions & 0 deletions external/js/cothority/spec/skipchain/skipchain-rpc.spec.ts
Expand Up @@ -64,6 +64,8 @@ describe("SkipchainRPC Tests", () => {
// nodes in order.
for (const node of roster.list) {
const c = new RosterWSConnection(new Roster({list: [node]}), "");
c.setTimeout(999999);

const update = await new SkipchainRPC(c).getUpdateChain(genesis.hash, false);
expect(update.length).toBe(blocks);
}
Expand Down
9 changes: 7 additions & 2 deletions external/js/cothority/src/network/websocket.ts
Expand Up @@ -56,7 +56,8 @@ export class WebSocketConnection implements IConnection {
}

this.service = service;
this.timeout = 30 * 1000; // 30s by default
// 50s by default. Onet will close a connection after 1 minute.
this.timeout = 50 * 1000;
this.url = url;
}

Expand Down Expand Up @@ -117,15 +118,19 @@ export class WebSocketConnection implements IConnection {
Log.lvl4(`Socket: new WebSocket(${url.href})`);
const ws = factory(url.href);
const bytes = Buffer.from(message.$type.encode(message).finish());
const timer = setTimeout(() => ws.close(1000, "timeout"), this.timeout);
let timer = setTimeout(() => ws.close(1000, "timeout"), this.timeout);

ws.onOpen(() => {
Log.lvl3("Sending message to", url.href);
ws.send(bytes);
});

ws.onMessage((data: Buffer) => {

// clear the timer and set a new one
clearTimeout(timer);
timer = setTimeout(() => ws.close(1000, "timeout"), this.timeout);

const buf = Buffer.from(data);
Log.lvl4("Getting message with length:", buf.length);

Expand Down

0 comments on commit 707b3ca

Please sign in to comment.