From f54a4fb137fa0540bb206fff3f734172676caacd Mon Sep 17 00:00:00 2001 From: Markus Felten Date: Wed, 6 Nov 2019 23:34:40 +0100 Subject: [PATCH] fix: separate ws --- src/service-koa.mjs | 62 ------------------------------------------ src/ws.mjs | 42 ++++++++++++++++++++++++++++ tests/failure-test.mjs | 4 +-- 3 files changed, 43 insertions(+), 65 deletions(-) create mode 100644 src/ws.mjs diff --git a/src/service-koa.mjs b/src/service-koa.mjs index f9a06b85..b7058b61 100644 --- a/src/service-koa.mjs +++ b/src/service-koa.mjs @@ -1,6 +1,5 @@ import http from "http"; import https from "https"; -import url from "url"; import Koa from "koa"; import { mergeAttributes, createAttributes } from "model-attributes"; import { Service } from "@kronos-integration/service"; @@ -100,7 +99,6 @@ export class ServiceKOA extends Service { super(config, owner); Object.defineProperties(this, { - socketEndpoints: { value: {} }, koa: { value: new Koa() } }); @@ -147,33 +145,6 @@ export class ServiceKOA extends Service { return this.listen.address; } - addSocketEndpoint(ep) { - //this.addEndpoint(new SocketEndpoint(name, this)); - this.socketEndpoints[ep.path] = ep; - return ep; - } - - removeSocketEndpoint(ep) { - delete this.socketEndpoints[ep.path]; - } - - createSocketEndpoint(name, path) { - const thePath = path || name; - - let ep = this.socketEndpoints[thePath]; - if (ep === undefined) { - ep = this.addSocketEndpoint(new SocketEndpoint(name, this, path)); - } - return ep; - } - - endpointForSocketConnection(ws, req) { - const location = url.parse(req.url, true); - return this.socketEndpoints[location.path]; - // you might use location.query.access_token to authenticate or share sessions - // or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312) - } - async _start() { try { this.server = this.isSecure @@ -182,39 +153,10 @@ export class ServiceKOA extends Service { const server = this.server; - - /* - this.wss = new WebSocketServer({ - server: this.server - }); - - this.wss.on("connection", (ws, req) => { - const ep = this.endpointForSocketConnection(ws, req); - - if (ep) { - ep.open(ws); - ws.on("message", (message, flags) => { - try { - message = JSON.parse(message); - this.trace({ - endpoint: ep.toString(), - received: message - }); - ep.receive(message); - } catch (e) { - this.error(e); - } - }); - ws.on("close", () => ep.close(ws)); - } - }); -*/ - if (this.timeout !== undefined) { server.setTimeout(this.timeout * 1000); } - return new Promise((resolve, reject) => { this.trace(severity => `starting ${this.url}`); @@ -271,8 +213,4 @@ export class ServiceKOA extends Service { } } -function decode(val) { - if (val !== undefined) return decodeURIComponent(val); -} - export default ServiceKOA; diff --git a/src/ws.mjs b/src/ws.mjs new file mode 100644 index 00000000..8bdfbc9f --- /dev/null +++ b/src/ws.mjs @@ -0,0 +1,42 @@ +import { Service } from "@kronos-integration/service"; + +export class ServiceWS extends Service +{ + constructor(config, owner) { + super(config, owner); + + Object.defineProperties(this, { + socketEndpoints: { value: {} } + }); + } + + + addSocketEndpoint(ep) { + //this.addEndpoint(new SocketEndpoint(name, this)); + this.socketEndpoints[ep.path] = ep; + return ep; + } + + removeSocketEndpoint(ep) { + delete this.socketEndpoints[ep.path]; + } + + createSocketEndpoint(name, path) { + const thePath = path || name; + + let ep = this.socketEndpoints[thePath]; + if (ep === undefined) { + ep = this.addSocketEndpoint(new SocketEndpoint(name, this, path)); + } + return ep; + } + + endpointForSocketConnection(ws, req) { + const location = url.parse(req.url, true); + return this.socketEndpoints[location.path]; + // you might use location.query.access_token to authenticate or share sessions + // or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312) + } + + +} \ No newline at end of file diff --git a/tests/failure-test.mjs b/tests/failure-test.mjs index a3ab4e20..41b1f028 100644 --- a/tests/failure-test.mjs +++ b/tests/failure-test.mjs @@ -34,9 +34,7 @@ test("service-koa failures with already in use port", async t => { await t.throwsAsync(() => ks2.start(), Error, 'listen EADDRINUSE: address already in use :::1235'); - // await ks2.start(); - - //t.is(ks2.state, "failed"); + t.is(ks2.state, "failed"); await ks1.stop(); //await ks2.stop();