2024-10-01 01:58:44 +00:00
|
|
|
import m from "mithril";
|
2024-10-05 02:57:21 +00:00
|
|
|
import { createWebSocket } from "./websocket";
|
2024-10-01 02:07:05 +00:00
|
|
|
|
2024-10-08 12:42:15 +00:00
|
|
|
// on new message event, call m.redraw();
|
|
|
|
const messages = [];
|
|
|
|
const socket = createWebSocket();
|
|
|
|
|
|
|
|
socket.onmessage = function (event) {
|
|
|
|
if (event.type === "message") {
|
|
|
|
const message = JSON.parse(event.data);
|
|
|
|
messages.push(message);
|
|
|
|
m.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function newMessageText(name, message) {
|
2024-10-09 00:31:53 +00:00
|
|
|
return [
|
|
|
|
m("span.chatter", name + ": "),
|
|
|
|
m("span.chatter-message", message)
|
|
|
|
];
|
2024-10-08 12:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const MessageBox = {
|
|
|
|
view: function () {
|
2024-10-24 01:57:58 +00:00
|
|
|
return m(".messages", { id: "message-box" }, messages.reverse().map(function(message) {
|
2024-10-08 12:42:15 +00:00
|
|
|
return m(".message", newMessageText(message.chatter, message.message));
|
2024-10-24 01:57:58 +00:00
|
|
|
}), m("#anchor"));
|
2024-10-08 12:42:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-24 01:57:58 +00:00
|
|
|
function clearMessageInputBox() {
|
2024-10-09 00:31:53 +00:00
|
|
|
const messageBox = document.getElementById("message");
|
|
|
|
messageBox.value = "";
|
|
|
|
}
|
|
|
|
|
2024-10-08 12:42:15 +00:00
|
|
|
function sendMessage(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2024-10-09 00:31:53 +00:00
|
|
|
const form = new FormData(e.target);
|
2024-10-08 12:42:15 +00:00
|
|
|
const message = {};
|
|
|
|
|
|
|
|
for (const [key, value] of form.entries()) {
|
|
|
|
message[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify(message));
|
2024-10-24 01:57:58 +00:00
|
|
|
clearMessageInputBox();
|
2024-10-08 12:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const MessageInput = {
|
|
|
|
view: function () {
|
2024-10-09 00:31:53 +00:00
|
|
|
return m("div.chat-input", [
|
2024-10-29 01:47:30 +00:00
|
|
|
m("form", { method: "post", onsubmit: this.handleSubmit }, [
|
2024-10-08 12:42:15 +00:00
|
|
|
m("input", { name: "chatter", placeholder: "Name" }),
|
2024-10-09 00:31:53 +00:00
|
|
|
m("input", { name: "message", placeholder: "Message", id: "message", required: true }),
|
2024-10-08 12:42:15 +00:00
|
|
|
m("input", { type: "submit", value: "Send" })
|
|
|
|
])
|
|
|
|
]);
|
2024-10-29 01:47:30 +00:00
|
|
|
},
|
|
|
|
nameHidden: false,
|
|
|
|
handleSubmit: function (e) {
|
|
|
|
sendMessage(e);
|
|
|
|
if (!this.nameHidden) {
|
|
|
|
const chatterInput = document.querySelector('input[name="chatter"]')
|
|
|
|
chatterInput.setAttribute('style', 'display: none');
|
|
|
|
this.nameHidden = true;
|
|
|
|
}
|
2024-10-08 12:42:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const Chat = {
|
|
|
|
view: function(vnode) {
|
2024-10-29 01:47:30 +00:00
|
|
|
return m("div", { class: 'container' }, [
|
2024-10-08 12:42:15 +00:00
|
|
|
m(MessageBox),
|
|
|
|
m(MessageInput)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
m.mount(document.getElementById("app"), Chat);
|