Skip to content

Commit

Permalink
fix: SocketStream is not imported
Browse files Browse the repository at this point in the history
  • Loading branch information
geforseN committed Dec 8, 2024
1 parent f0e036e commit d602d4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
33 changes: 7 additions & 26 deletions src/module/Chat/initializeChatNamespace.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
import type { SocketStream } from "@fastify/websocket";
import type { WebSocket } from "@fastify/websocket";
import type { FastifyRequest } from "fastify";
import type { WebSocket } from "ws";
import {
SocketsStore,
defaultListeners,
type CustomWebsocketEvent,
} from "../../ws/index.js";
import { SocketsStore, defaultListeners } from "../../ws/index.js";
import { Chat } from "./entity/index.js";

export interface AdditionalListeners {
addListener(
event: "socket",
listener: (this: WebSocket, event: CustomWebsocketEvent) => void,
): this;
on(
event: "socket",
listener: (this: WebSocket, event: CustomWebsocketEvent) => void,
): this;
}

export default function initializeChat() {
const socketsStore = new SocketsStore();
const chat = new Chat({ socketsStore });

return function getChatContext(
connection: SocketStream & { socket: WebSocket & AdditionalListeners },
request: FastifyRequest,
) {
socketsStore.add(connection.socket);
socketsStore.room(request.session.user.id).add(connection.socket);
connection.socket
return function getChatContext(socket: WebSocket, request: FastifyRequest) {
socketsStore.add(socket);
socketsStore.room(request.session.user.id).add(socket);
socket
.addListener("socket", defaultListeners.socket)
.addListener("message", defaultListeners.message);
return {
sender: request.session.user,
socket: connection.socket,
socket,
chat,
socketsStore,
};
Expand Down
15 changes: 6 additions & 9 deletions src/module/Lobbies/lobbies.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert";
import type { SocketStream } from "@fastify/websocket";
import type { WebSocket } from "@fastify/websocket";
import type { FastifyInstance, FastifyRequest } from "fastify";
import type { InitialGameSettings } from "@durak-game/durak-dts";
import {
Expand Down Expand Up @@ -96,18 +96,15 @@ function initializeGameLobbies() {
const socketsStore = new SocketsStore();
const lobbies = new Lobbies(socketsStore);

return function handleConnection(
connection: SocketStream,
request: FastifyRequest,
) {
socketsStore.add(connection.socket);
socketsStore.room(request.session.user.id).add(connection.socket);
connection.socket
return function handleConnection(socket: WebSocket, request: FastifyRequest) {
socketsStore.add(socket);
socketsStore.room(request.session.user.id).add(socket);
socket
.addListener("message", defaultListeners.message)
.addListener("socket", defaultListeners.socket);
return {
user: request.session.user,
socket: connection.socket,
socket,
lobbies,
};
};
Expand Down

0 comments on commit d602d4d

Please sign in to comment.