Add some logging to the event handlers for the chat room, add styles provided by my wife

This commit is contained in:
Dave Smith-Hayes 2024-10-27 22:43:29 -04:00
parent d891feb90d
commit f6947f32f1
4 changed files with 80 additions and 28 deletions

View File

@ -47,4 +47,5 @@ export class ChatRoom extends EventEmitter {
} }
} }
export const chatRoom = new ChatRoom(); const chatRoom = new ChatRoom();
export { chatRoom }

View File

@ -14,12 +14,18 @@ const { upgradeWebSocket, websocket } = createBunWebSocket<ServerWebSocket>();
app.get("/chat-service", upgradeWebSocket((c) => { app.get("/chat-service", upgradeWebSocket((c) => {
return { return {
onOpen(event, ws) { onOpen(event, ws) {
chatRoom.addListener('message-added', function (e) { console.log(ws);
ws.send(JSON.stringify({ console.log(`Connection established ${event}`);
chatRoom.addListener('message-added', async function (e) {
const message = JSON.stringify({
message: e.message.message, message: e.message.message,
chatter: e.message.chatter, chatter: e.message.chatter,
timestamp: e.message.timestamp timestamp: e.message.timestamp
})); });
console.log(message);
ws.send(message);
}); });
}, },
onMessage(event, ws) { onMessage(event, ws) {
@ -39,19 +45,6 @@ app.get("/chat-service", upgradeWebSocket((c) => {
} }
})); }));
// set up the username, somehow
// set the session for a user
app.post("/login", async (c) => {
const user = await c.req.parseBody();
let success: boolean = false;
if (!users.find((u) => u == user.username)) {
users.push(user.username);
success = true;
}
return c.json({ success });
});
// get the HTML and JS from the static repo // get the HTML and JS from the static repo
app.use("/", serveStatic({ path: "/static/index.html" })); app.use("/", serveStatic({ path: "/static/index.html" }));

View File

@ -1,7 +1,8 @@
body { body {
margin: 0; margin: 0;
padding: 0; padding: 0;
height: 100.001vh; background-color: #134115;
font-family: monospace;
} }
div { div {
@ -12,15 +13,19 @@ div {
padding: 1em; padding: 1em;
display: flex; display: flex;
flex-direction: column-reverse; flex-direction: column-reverse;
height: 95vh; height: auto;
overflow: auto; overflow: auto;
} }
.messages .message { .messages .message {
padding: 0.5em; padding: 1em 1.25em;
border: 1px solid #aaa; border: 1px solid #020a03;
border-radius: 5px; border-radius: 5px;
margin-bottom: 0.5em; margin-bottom: 0.75em;
background-color: #fffefc;
font-family: monospace;
font-size: 14px;
color: #0c0b09;
} }
.chatter { .chatter {
@ -32,10 +37,10 @@ div {
bottom: 0; bottom: 0;
width: 100%; width: 100%;
box-sizing: border-box; box-sizing: border-box;
padding: 0.5em;
background-color: #fff;
border-top: 1px solid #ccc; border-top: 1px solid #ccc;
height: 5vh; height: auto;
padding: 1em;
background-color: #f4efe6;
} }
.chat-input > form { .chat-input > form {
display: flex; display: flex;
@ -44,12 +49,65 @@ div {
} }
.chat-input > form > input, .chat-input > form > input,
.chat-input > form > submit { .chat-input > form > submit {
padding: 0.7em; padding: 1em 1.25em;
margin-right: 0.4em; margin-right: 1em;
}
.chat-input > form > submit {
background-color: #020a03;
border: none;
border-radius: 5px;
color: #fffbf6;
font-weight: bold;
font-family: monospace !important;
padding: 0.75em 1em;
margin-right: 0;
} }
.chat-input > form > input[name="chatter"] { .chat-input > form > input[name="chatter"] {
width: 20%; width: 20%;
border: 1px solid #020a03;
border-radius: 5px;
color: #0c0b09;
font-family: monospace;
} }
.chat-input > form > input[name="message"] { .chat-input > form > input[name="message"] {
width: 75%; width: 75%;
border: 1px solid #020a03;
border-radius: 5px;
color: #0c0b09;
font-family: monospace;
}
@media only screen and (max-width: 600px) {
.chat-input {
padding: 2.5em;
}
.chat-input > form {
display: flex;
width: 100%;
box-sizing: border-box;
flex-direction: column;
}
.chat-input > form > input[name="chatter"] {
width: 100%;
font-size: 3.5em;
margin-bottom: 0.5em;
box-sizing: border-box;
}
.chat-input > form > input[name="message"] {
width: 100%;
font-size: 3.5em;
margin-bottom: 0.5em;
box-sizing: border-box;
}
.chat-input > form > input, .chat-input > form > submit {
padding: 0.75em 1em;
border-radius: 0.25em;
width: 100%;
font-size: 3.5em;
}
} }

View File

@ -1721,7 +1721,7 @@ var import_mithril = __toESM(require_mithril(), 1);
// frontend/websocket.js // frontend/websocket.js
function createWebSocket() { function createWebSocket() {
let appUrl = "undefined"; let appUrl = "chat.freedoom.party";
appUrl = `//${appUrl}/chat-service`; appUrl = `//${appUrl}/chat-service`;
return new WebSocket(appUrl); return new WebSocket(appUrl);
} }