Skip to content

Commit

Permalink
client should have only one agent
Browse files Browse the repository at this point in the history
Signed-off-by: Jari Kolehmainen <[email protected]>
  • Loading branch information
jakolehm committed Feb 17, 2021
1 parent b1c6cb8 commit 489b1ff
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/lens-client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import WebSocket from "ws";
import { LensAgent } from "./lens-agent";

export class LensClient {
public socket: WebSocket;
public readonly socket: WebSocket;
public readonly agent: LensAgent;

constructor(socket: WebSocket) {
constructor(socket: WebSocket, agent: LensAgent) {
this.socket = socket;
this.agent = agent;
}
}
39 changes: 29 additions & 10 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { IncomingMessage } from "http";
import { LensClient } from "./lens-client";
import { LensAgent } from "./lens-agent";
import { URL } from "url";
import { version } from "../package.json";

export class SignalingServer {
private ws?: Server;
private clients: LensClient[] = [];
private agents: LensAgent[] = [];

start(port = 8080) {
console.log("~~ Heliograph ~~");
console.log(`~~ Heliograph v${version} ~~`);

this.ws = new Server({
port
Expand Down Expand Up @@ -43,14 +44,21 @@ export class SignalingServer {
this.ws?.close();
}

private handleClientSocket(socket: WebSocket) {
console.log("client connected", socket.extensions);
this.clients.push(new LensClient(socket));
public handleClientSocket(socket: WebSocket) {
const agent = this.getAgentForClient();

if (!agent) {
socket.close();

return;
}

console.log("client connected");
const client = new LensClient(socket, agent);

this.clients.push(client);
socket.on("message", (data) => {
this.agents.forEach((agent) => {
agent.socket.send(data);
});
client.agent.socket.send(data);
});

socket.on("close", () => {
Expand All @@ -62,23 +70,34 @@ export class SignalingServer {
});
}

private handleAgentSocket(socket: WebSocket) {
public handleAgentSocket(socket: WebSocket) {
console.log("agent connected");
this.agents.push(new LensAgent(socket));
const agent = new LensAgent(socket);

this.agents.push(agent);

socket.on("message", (data) => {
this.clients.forEach((client) => {
this.getClientsForAgent(agent).forEach((client) => {
client.socket.send(data);
});
});

socket.on("close", () => {
console.log("agent closed");
this.getClientsForAgent(agent).forEach((client) => client.socket.close());
const index = this.agents.findIndex((agent) => agent.socket === socket);

if (index !== -1) {
this.agents.splice(index, 1);
}
});
}

public getClientsForAgent(agent: LensAgent) {
return this.clients.filter((client) => client.agent === agent);
}

public getAgentForClient(): LensAgent {
return this.agents[Math.floor(Math.random() * this.agents.length)];
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"resolveJsonModule": true,
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
Expand Down

0 comments on commit 489b1ff

Please sign in to comment.