feat(base): Fixed logic for websockets and added GameStateEvent. Might've caused instability on other feature branches! (#84)
Reviewed-on: #84 Reviewed-by: lq64 <lq@blackhole.local> Co-authored-by: Janis <janis.e.20@gmx.de> Co-committed-by: Janis <janis.e.20@gmx.de>
This commit is contained in:
@@ -39,4 +39,12 @@ function receiveHandEvent(eventData) {
|
||||
handElement.html(newHtml);
|
||||
}
|
||||
|
||||
onEvent("ReceivedHandEvent", receiveHandEvent)
|
||||
function receiveGameStateChange(eventData) {
|
||||
const content = eventData.content;
|
||||
const title = eventData.title || 'Knockout Whist';
|
||||
|
||||
exchangeBody(content, title);
|
||||
}
|
||||
|
||||
onEvent("ReceivedHandEvent", receiveHandEvent)
|
||||
onEvent("GameStateChangeEvent", receiveGameStateChange)
|
||||
@@ -216,4 +216,7 @@ function navSpa(page, title) {
|
||||
})
|
||||
});
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
globalThis.exchangeBody = exchangeBody;
|
||||
@@ -1,10 +1,7 @@
|
||||
type EventHandler = (data: any) => any | Promise<any>;
|
||||
|
||||
// javascript
|
||||
let ws = null; // will be created by connectWebSocket()
|
||||
const pending: Map<string, any> = new Map(); // id -> { resolve, reject, timer }
|
||||
const handlers: Map<string, EventHandler> = new Map(); // eventType -> handler(data) -> (value|Promise)
|
||||
|
||||
const pending = new Map(); // id -> { resolve, reject, timer }
|
||||
const handlers = new Map(); // eventType -> handler(data) -> (value|Promise)
|
||||
|
||||
let timer = null;
|
||||
|
||||
@@ -52,6 +49,7 @@ function setupSocketHandlers(socket) {
|
||||
|
||||
if (!handler) {
|
||||
// no handler: respond with an error object in data so server can fail it
|
||||
console.warn("No handler for event:", eventType);
|
||||
sendResponse({error: "No handler for event: " + eventType});
|
||||
return;
|
||||
}
|
||||
@@ -182,7 +180,7 @@ function sendEventAndWait(eventType, eventData, timeoutMs = 10000) {
|
||||
return p;
|
||||
}
|
||||
|
||||
function onEvent(eventType: string, handler: EventHandler) {
|
||||
function onEvent(eventType, handler) {
|
||||
handlers.set(eventType, handler);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user