Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
subfuzion committed Aug 11, 2022
1 parent 6cdc0c5 commit 09576f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
26 changes: 20 additions & 6 deletions src/server/pong/client.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import {EventEmitter} from "events";
import {WebSocket} from "ws";
import {v4 as uuid} from "uuid";
import {Message} from "../../common/pong/messages.js";


/**
* A Client wraps a websocket.
* A Client is a light wrapper over a websocket with a unique ID.
*/
export class Client extends EventEmitter {
public readonly ws: WebSocket;
public readonly id: string;
readonly ws: WebSocket;
readonly id: string;

constructor(ws: WebSocket) {
super();
this.ws = ws;
this.id = uuid();
}

send(data: any, cb?: (err?: Error) => void): void {
this.ws.send(data, cb);
/**
* Data are sent with no callbacks.
* @param data Stringified JSON.
*/
send(data: string): void {
this.ws.send(data);
}

/**
* Messages are sent with no callbacks.
* @param m An instance of Message (or one of its subclasses).
*/
sendMessage(m: Message): void {
this.ws.send(JSON.stringify(m));
}
}
}
26 changes: 16 additions & 10 deletions src/server/pong/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,36 @@ export class Connections extends EventEmitter {
}

// Simple broadcast to all clients.
broadcast(message: any): void {
broadcast(message: string): void {
for (const client of this.connections.values()) {
client.send(message, (err) => {
console.log(err);
});
client.send(message);
}
}

// Broadcast to all clients using a generator function.
broadcastg(g: Generator<[Client, any]>): void {
broadcastg(g: Generator<[Client, string]>): void {
for (const [client, message] of g) {
client.send(message, err => {
if (err) console.log(err);
});
client.send(message);
}
}

startBroadcasting(
g: () => Generator<[Client, any]>,
g: () => Generator<[Client, string]>,
intervalMs: number): void {
this.intervalId = setInterval(() => this.broadcastg(g()), intervalMs);
}

stopBroadcasting() {
stopBroadcasting(): void {
clearInterval(this.intervalId);
}

close(): Promise<void> {
return new Promise(resolve => {
this.stopBroadcasting();
for (const [ws] of this.connections) {
ws.close();
}
resolve();
});
}
}
6 changes: 4 additions & 2 deletions src/server/pong/pongserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class PongServer {
}

async close(): Promise<void> {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
await this.connections.close();
this.server.close(err => {
if (err) return reject(err);
resolve();
Expand Down Expand Up @@ -91,7 +92,8 @@ export class PongServer {

game.onStateChange((m: Message) => {
// console.log(m);
ws.send(JSON.stringify(m));
//ws.send(JSON.stringify(m));
client.sendMessage(m);
});

ws.on("message", data => {
Expand Down

0 comments on commit 09576f0

Please sign in to comment.