-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters