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, req: NextApiRequest,
res: NextApiResponse res: NextApiResponse
) { ) {
if (res.socket.server.io) {
console.log("Socket is already running");
res.end();
return;
}
console.log("Socket is initializing"); const receiptId = req.body;
const io = new Server(res.socket.server);
res.socket.server.io = io;
io.on("connection", (socket) => { console.log(receiptId);
console.log("Received new connection");
socket.on("input-change", (msg) => { // if (res.socket?.server.io) {
socket.broadcast.emit("update-input", msg); // 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(); res.end();
} }

View file

@ -27,17 +27,25 @@ const ReceiptPage: NextPage = () => {
const [calculated] = useAtom(receiptTotalAtom); const [calculated] = useAtom(receiptTotalAtom);
const [unwrappedReceipt] = useAtom(unwrappedReceiptAtom); const [unwrappedReceipt] = useAtom(unwrappedReceiptAtom);
const { id } = router.query; const { pathname } = router;
// Connect to the socket server // Connect to the socket server
useEffect(() => { useEffect(() => {
const socketInitializer = async () => {
await fetch("/api/socket");
socket = io();
socket.on("connect", () => { console.log(pathname);
console.log("connected"); 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(); socketInitializer();