Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Implemented Peerjs Custom Events #457

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { Api } from "./api/index.ts";
import type { IClient } from "./models/client.ts";
import type { IMessage } from "./models/message.ts";
import type { IConfig } from "./config/index.ts";
import { MessageType } from "./enums.ts";
import type WebSocket from "ws";

export interface PeerServerEvents {
on(event: "connection", listener: (client: IClient) => void): this;
Expand All @@ -24,7 +26,9 @@ export interface PeerServerEvents {
// eslint-disable-next-line @typescript-eslint/unified-signatures
on(event: "disconnect", listener: (client: IClient) => void): this;
on(event: "error", listener: (client: Error) => void): this;
on(event: string, listener: (socket: WebSocket, message: IMessage) => void): this;
}
type CustomEmitFn = (type: string, payload: object) => void;

export const createInstance = ({
app,
Expand All @@ -38,6 +42,7 @@ export const createInstance = ({
const config = options;
const realm: IRealm = new Realm();
const messageHandler = new MessageHandler(realm);
const defaultMessageType:Set<MessageType> = new Set(Object.values(MessageType));

const api = Api({ config, realm, corsOptions: options.corsOptions });
const messagesExpire: IMessagesExpire = new MessagesExpire({
Expand Down Expand Up @@ -82,9 +87,14 @@ export const createInstance = ({
app.emit("connection", client);
});

wss.on("message", (client: IClient, message: IMessage) => {
app.emit("message", client, message);
messageHandler.handle(client, message);
wss.on("message", (client: IClient, message: IMessage,socket: CustomEmitFn) => {
if(defaultMessageType.has(message.type)){
app.emit("message", client, message);
messageHandler.handle(client, message);
} else {
app.emit(message.type,socket,message.payload)
}

});

wss.on("close", (client: IClient) => {
Expand Down
2 changes: 1 addition & 1 deletion src/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export interface IMessage {
readonly type: MessageType;
readonly src: string;
readonly dst: string;
readonly payload?: string | undefined;
readonly payload?: string | object | undefined;
}
8 changes: 6 additions & 2 deletions src/services/webSocketServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
const message = JSON.parse(data.toString()) as Writable<IMessage>;

message.src = client.getId();

this.emit("message", client, message);
const _emit = this.customEmit.bind(null, socket);
this.emit("message", client, message, { _emit } );
} catch (e) {
this.emit("error", e);
}
Expand All @@ -164,6 +164,10 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
this.emit("connection", client);
}

customEmit(socket: WebSocket, type: string,payload:object): void {
socket.send(JSON.stringify({ type, payload }))
}

private _sendErrorAndClose(socket: WebSocket, msg: Errors): void {
socket.send(
JSON.stringify({
Expand Down