[wip] so close; check the ws messages
This commit is contained in:
@@ -6,6 +6,7 @@ import { combine } from "kefir";
|
||||
import Bus from "kefir-bus";
|
||||
import db from "./db";
|
||||
import { liveTable, WsIn, WsOut } from "./table";
|
||||
import { err } from "./logging";
|
||||
|
||||
export const WS = Bus<
|
||||
{
|
||||
@@ -76,13 +77,17 @@ const api = new Elysia({ prefix: "/api" })
|
||||
})
|
||||
)
|
||||
.ws("/ws/:tableKey", {
|
||||
async open({
|
||||
body: WsIn,
|
||||
response: WsOut,
|
||||
|
||||
open: ({
|
||||
data: {
|
||||
params: { tableKey },
|
||||
humanKey,
|
||||
},
|
||||
send,
|
||||
}) {
|
||||
}) => {
|
||||
console.log("websocket opened");
|
||||
const table = liveTable(tableKey);
|
||||
|
||||
table.inputs.connectionChanges.emit({
|
||||
@@ -90,22 +95,14 @@ const api = new Elysia({ prefix: "/api" })
|
||||
presence: "joined",
|
||||
});
|
||||
|
||||
Object.entries(table.outputs.global).forEach(([type, stream]) =>
|
||||
Object.entries({
|
||||
...table.outputs.global,
|
||||
...(table.outputs.player[humanKey] ?? {}),
|
||||
}).forEach(([type, stream]) =>
|
||||
stream.onValue((v) => send({ [type]: v }))
|
||||
);
|
||||
|
||||
combine(
|
||||
[table.outputs.gameState],
|
||||
[table.outputs.gameImpl],
|
||||
(state, game: Game) => state && game.getView({ state, humanKey })
|
||||
)
|
||||
.toProperty()
|
||||
.onValue((view) => send({ view }));
|
||||
},
|
||||
|
||||
body: WsIn,
|
||||
response: WsOut,
|
||||
|
||||
message: (
|
||||
{
|
||||
data: {
|
||||
@@ -114,19 +111,20 @@ const api = new Elysia({ prefix: "/api" })
|
||||
},
|
||||
},
|
||||
body
|
||||
) => WS.emit({ ...body, type: "message", humanKey, tableKey }),
|
||||
) => liveTable(tableKey).inputs.messages.emit({ ...body, humanKey }),
|
||||
|
||||
close({
|
||||
close: ({
|
||||
data: {
|
||||
params: { tableKey },
|
||||
humanKey,
|
||||
},
|
||||
}) {
|
||||
}) =>
|
||||
liveTable(tableKey).inputs.connectionChanges.emit({
|
||||
humanKey,
|
||||
presence: "left",
|
||||
});
|
||||
},
|
||||
}),
|
||||
|
||||
error: (error) => err(error),
|
||||
});
|
||||
|
||||
export default api;
|
||||
|
||||
Reference in New Issue
Block a user