Skip to content

Commit

Permalink
update server setup
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshidwivedi committed Jun 18, 2024
1 parent b73f7ea commit 9e8b53d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createServer } from "http";

import express from "express";
import next, { NextApiHandler } from "next";
import {Server} from "socket.io";

const port= parseInt(process.env.PORT || "3000", 10);
const dev = process.env.NODE_ENV !== "production";
Expand All @@ -12,6 +13,26 @@ 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!");
});

io.on("connection", (socket)=>{
console.log("connection");

socket.on("draw", (moves, options)=>{
console.log("drawing");
socket.broadcast.emit("socket_draw", moves, options);
});

socket.on("disconnect", ()=>{
console.log("disconnected");
});

});

app.all("*", (req, res) => nextHandler(req, res));
server.listen(port, () =>{
console.log(`Server is ready on ${port}`);
Expand Down

0 comments on commit 9e8b53d

Please sign in to comment.