From efcc311582e1f1869884b42a9845ae94b9e4fd77 Mon Sep 17 00:00:00 2001 From: Deveshi Dwivedi Date: Thu, 20 Jun 2024 13:42:00 +0530 Subject: [PATCH] update to fix errors --- modules/room/components/MousePosition.tsx | 2 +- modules/room/hooks/useBoardPosition.ts | 1 + server/index.ts | 67 ++++++++++++----------- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/modules/room/components/MousePosition.tsx b/modules/room/components/MousePosition.tsx index 414f01b..9ccbbc7 100644 --- a/modules/room/components/MousePosition.tsx +++ b/modules/room/components/MousePosition.tsx @@ -15,7 +15,7 @@ export const MousePosition = () => { useInterval(()=>{ if(prevPosition.current.x !== docX || prevPosition.current.y !== docY){ - socket.emit("mouse_move", docX- x.get(), docY- y.getY()); + socket.emit("mouse_move", docX- x.get(), docY- y.get()); prevPosition.current = {x:docX, y:docY}; } }, 300); diff --git a/modules/room/hooks/useBoardPosition.ts b/modules/room/hooks/useBoardPosition.ts index f35c145..953199b 100644 --- a/modules/room/hooks/useBoardPosition.ts +++ b/modules/room/hooks/useBoardPosition.ts @@ -1,5 +1,6 @@ import { useContext } from "react"; +import { roomContext } from "../context/Room.context"; export const useBoardPosition = () => { const { x, y } = useContext(roomContext); diff --git a/server/index.ts b/server/index.ts index a202581..288ebf6 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,48 +1,49 @@ + import { createServer } from "http"; +import {} from "@/common/types/global" import express from "express"; -import next from "next"; -import { Server as SocketIOServer, Socket as SocketIOSocket } from "socket.io"; +import next, { NextApiHandler } from "next"; +import {Server} from "socket.io"; -const port = parseInt(process.env.PORT || "3000", 10); +const port= parseInt(process.env.PORT || "3000", 10); const dev = process.env.NODE_ENV !== "production"; -const nextApp = next({ dev }); -const nextHandler = nextApp.getRequestHandler(); - -nextApp.prepare().then(async () => { - const app = express(); - const server = createServer(app); +const nextApp= next({ dev }); +const nextHandler= nextApp.getRequestHandler(); - const io = new SocketIOServer(server); +nextApp.prepare().then(async() =>{ +const app= express(); +const server= createServer(app); +const io= new Server(server); - app.get("/hello", async (_, res) => { - res.send("Hello ji!"); - }); +app.get("/hello", async(_, res)=>{ + res.send("Hello ji!"); +}); - io.on("connection", (socket: SocketIOSocket) => { - console.log("connection"); +io.on("connection", (socket)=>{ + console.log("connection"); - const allUsers= io.sockets.adapter.rooms.get("global"); + const allUsers= io.sockets.adapter.rooms.get("global"); if (allUsers) io.to("global").emit("users_in_room", [...allUsers]); + socket.on("draw", (moves, options)=>{ + console.log("drawing"); + socket.broadcast.emit("socket_draw", moves, options); + }); - socket.on("draw", (moves, options) => { - console.log("drawing"); - socket.broadcast.emit("socket_draw", moves, options); - }); - - socket.on("mouse_move", (x,y) => { - console.log("mouse move"); - socket.broadcast.emit("mouse_moved", x, y, socket.id); - - }) - - socket.on("disconnect", () => { - console.log("disconnected"); - }); + socket.on("mouse_move", (x,y) => { + console.log("mouse move"); + socket.broadcast.emit("mouse_moved", x, y, socket.id); + }); - app.all("*", (req, res) => nextHandler(req, res)); - server.listen(port, () => { - console.log(`Server is ready on ${port}`); + socket.on("disconnect", ()=>{ + console.log("disconnected"); }); + +}); + +app.all("*", (req, res) => nextHandler(req, res)); +server.listen(port, () =>{ + console.log(`Server is ready on ${port}`); }); +}); \ No newline at end of file