psuhign what I have

This commit is contained in:
Devin Deng 2022-11-08 21:20:15 -08:00
parent fa704809ec
commit eedf038963
2 changed files with 35 additions and 20 deletions

View file

@ -7,22 +7,29 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (res.socket.server.io) {
console.log("Socket is already running");
res.end();
return;
}
console.log("Socket is initializing");
const io = new Server(res.socket.server);
res.socket.server.io = io;
const receiptId = req.body;
io.on("connection", (socket) => {
console.log("Received new connection");
console.log(receiptId);
socket.on("input-change", (msg) => {
socket.broadcast.emit("update-input", msg);
});
});
// if (res.socket?.server.io) {
// console.log("Socket is already running");
// res.end();
// return;
// }
// console.log("Socket is initializing");
// const io = new Server(res.socket.server);
// res.socket.server.io = io;
// io.on("connection", (socket) => {
// console.log("Received new connection");
// socket.on("input-change", (msg) => {
// socket.broadcast.emit("update-input", msg);
// });
// });
res.status(200);
res.end();
}

View file

@ -27,17 +27,25 @@ const ReceiptPage: NextPage = () => {
const [calculated] = useAtom(receiptTotalAtom);
const [unwrappedReceipt] = useAtom(unwrappedReceiptAtom);
const { id } = router.query;
const { pathname } = router;
// Connect to the socket server
useEffect(() => {
const socketInitializer = async () => {
await fetch("/api/socket");
socket = io();
socket.on("connect", () => {
console.log("connected");
console.log(pathname);
const socketInitializer = async () => {
await fetch("/api/socket", {
method: "POST",
headers: {
"Content-Type": 'application/json',
},
body: JSON.stringify({ receiptId: id }),
});
// socket = io();
// socket.on("connect", () => {
// console.log("connected");
// });
};
socketInitializer();