Skip to content

Commit

Permalink
fix: separate ws
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Nov 6, 2019
1 parent 8c9123a commit f54a4fb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 65 deletions.
62 changes: 0 additions & 62 deletions src/service-koa.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -100,7 +99,6 @@ export class ServiceKOA extends Service {
super(config, owner);

Object.defineProperties(this, {
socketEndpoints: { value: {} },
koa: { value: new Koa() }
});

Expand Down Expand Up @@ -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
Expand All @@ -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}`);

Expand Down Expand Up @@ -271,8 +213,4 @@ export class ServiceKOA extends Service {
}
}

function decode(val) {
if (val !== undefined) return decodeURIComponent(val);
}

export default ServiceKOA;
42 changes: 42 additions & 0 deletions src/ws.mjs
Original file line number Diff line number Diff line change
@@ -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)
}


}
4 changes: 1 addition & 3 deletions tests/failure-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f54a4fb

Please sign in to comment.