-
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
1 parent
ef0fe22
commit efcc311
Showing
3 changed files
with
36 additions
and
34 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
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
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}`); | ||
}); | ||
}); |