Skip to content

Commit

Permalink
update to fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshidwivedi committed Jun 20, 2024
1 parent ef0fe22 commit efcc311
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion modules/room/components/MousePosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions modules/room/hooks/useBoardPosition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { useContext } from "react";
import { roomContext } from "../context/Room.context";

export const useBoardPosition = () => {
const { x, y } = useContext(roomContext);
Expand Down
67 changes: 34 additions & 33 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -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<ClientToServerEvents, ServerToClientEvents>(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}`);
});
});

0 comments on commit efcc311

Please sign in to comment.