forward events received from the game client to the front-end client

This commit is contained in:
2025-07-28 12:36:50 +02:00
parent 4395b975ef
commit 6344142fdd
5 changed files with 104 additions and 25 deletions

View File

@@ -6,6 +6,8 @@ import { auth } from "./lib/auth";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { logger } from "hono/logger";
import * as http from "node:http";
import { Server } from 'socket.io';
const app = new Hono();
@@ -33,4 +35,38 @@ app.get("/", (c) => {
return c.text("OK");
});
app.post("/", async (c) => {
const data = await c.req.json();
console.log('post received', 'body: ', data);
// Forward the received data to all connected Socket.IO clients
io.emit('event', data);
return c.text("OK");
});
const httpServer = http.createServer();
const io = new Server(httpServer,{ cors: {
origin: process.env.CORS_ORIGIN || "",
methods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
}});
io.on('connection', (socket) => {
console.log('it might work OMG');
socket.emit('hello', 'world');
socket.on('hello', (data) => {
socket.broadcast.emit('hello', data);
});
socket.on('message', (data) => {
console.log('message received', data);
});
});
httpServer.listen(4000, () => {
console.log('ws listening on *:4000');
});
export default app;