-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,33 +34,56 @@ | |
}); | ||
}; | ||
|
||
const format = async () => { | ||
for (const message of store.messages) { | ||
if (!message._fmt && message.text.startsWith("{")) { | ||
message._fmt = true; | ||
const { codeToHtml } = await import("https://esm.sh/[email protected]"); | ||
const str = JSON.stringify(JSON.parse(message.text), null, 2); | ||
message.formattedText = await codeToHtml(str, { | ||
lang: "json", | ||
theme: "dark-plus", | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const log = (user, ...args) => { | ||
console.log("[ws]", user, ...args); | ||
store.messages.push({ | ||
text: args.join(" "), | ||
formattedText: "", | ||
user: user, | ||
date: new Date().toLocaleString(), | ||
}); | ||
scroll(); | ||
format(); | ||
}; | ||
|
||
const connect = async () => { | ||
const isSecure = location.protocol === "https:"; | ||
const url = (isSecure ? "wss://" : "ws://") + location.host + "/_ws"; | ||
if (ws) { | ||
log("system", "closing"); | ||
log("system", "Closing previous connection before reconnecting..."); | ||
ws.close(); | ||
clear(); | ||
} | ||
|
||
log("system", "Connecting to", url, "..."); | ||
ws = new WebSocket(url); | ||
|
||
ws.addEventListener("message", (event) => { | ||
log("server", event.data); | ||
const { user = "system", message = "" } = event.data.startsWith("{") | ||
? JSON.parse(event.data) | ||
: { message: event.data }; | ||
log( | ||
user, | ||
typeof message === "string" ? message : JSON.stringify(message), | ||
); | ||
}); | ||
|
||
log("system", "Waiting for connection..."); | ||
await new Promise((resolve) => ws.addEventListener("open", resolve)); | ||
log("system", "Connected!"); | ||
}; | ||
|
||
const clear = () => { | ||
|
@@ -71,7 +94,6 @@ | |
const send = () => { | ||
console.log("sending message..."); | ||
if (store.message) { | ||
log("you", store.message); | ||
ws.send(store.message); | ||
} | ||
store.message = ""; | ||
|
@@ -92,7 +114,6 @@ | |
|
||
await connect(); | ||
ping(); | ||
send("foo"); | ||
</script> | ||
</head> | ||
<body class="h-screen flex flex-col justify-between"> | ||
|
@@ -108,8 +129,16 @@ | |
alt="Avatar" | ||
class="w-8 h-8 rounded-full" | ||
/> | ||
<div class="ml-2 bg-gray-800 rounded-lg p-2"> | ||
<p class="text-white">{{ message.text }}</p> | ||
<div | ||
class="ml-2 bg-gray-800 rounded-lg p-2" | ||
style="max-width: 33.33vw" | ||
> | ||
<p | ||
v-if="message.formattedText" | ||
class="overflow-x-scroll" | ||
v-html="message.formattedText" | ||
></p> | ||
<p v-else class="text-white">{{ message.text }}</p> | ||
</div> | ||
</div> | ||
<p class="text-gray-500 mt-1 text-xs ml-10">{{ message.date }}</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export default /* html */ `<!doctype html> | ||
export default /* html */ ` | ||
<!doctype html> | ||
<html lang="en" data-theme="dark"> | ||
<head> | ||
<title>CrossWS Test Page</title> | ||
|
@@ -34,33 +35,56 @@ export default /* html */ `<!doctype html> | |
}); | ||
}; | ||
const format = async () => { | ||
for (const message of store.messages) { | ||
if (!message._fmt && message.text.startsWith("{")) { | ||
message._fmt = true; | ||
const { codeToHtml } = await import("https://esm.sh/[email protected]"); | ||
const str = JSON.stringify(JSON.parse(message.text), null, 2); | ||
message.formattedText = await codeToHtml(str, { | ||
lang: "json", | ||
theme: "dark-plus", | ||
}); | ||
} | ||
} | ||
}; | ||
const log = (user, ...args) => { | ||
console.log("[ws]", user, ...args); | ||
store.messages.push({ | ||
text: args.join(" "), | ||
formattedText: "", | ||
user: user, | ||
date: new Date().toLocaleString(), | ||
}); | ||
scroll(); | ||
format(); | ||
}; | ||
const connect = async () => { | ||
const isSecure = location.protocol === "https:"; | ||
const url = (isSecure ? "wss://" : "ws://") + location.host + "/_ws"; | ||
if (ws) { | ||
log("system", "closing"); | ||
log("system", "Closing previous connection before reconnecting..."); | ||
ws.close(); | ||
clear(); | ||
} | ||
log("system", "Connecting to", url, "..."); | ||
ws = new WebSocket(url); | ||
ws.addEventListener("message", (event) => { | ||
log("server", event.data); | ||
const { user = "system", message = "" } = event.data.startsWith("{") | ||
? JSON.parse(event.data) | ||
: { message: event.data }; | ||
log( | ||
user, | ||
typeof message === "string" ? message : JSON.stringify(message), | ||
); | ||
}); | ||
log("system", "Waiting for connection..."); | ||
await new Promise((resolve) => ws.addEventListener("open", resolve)); | ||
log("system", "Connected!"); | ||
}; | ||
const clear = () => { | ||
|
@@ -71,7 +95,6 @@ export default /* html */ `<!doctype html> | |
const send = () => { | ||
console.log("sending message..."); | ||
if (store.message) { | ||
log("you", store.message); | ||
ws.send(store.message); | ||
} | ||
store.message = ""; | ||
|
@@ -92,7 +115,6 @@ export default /* html */ `<!doctype html> | |
await connect(); | ||
ping(); | ||
send("foo"); | ||
</script> | ||
</head> | ||
<body class="h-screen flex flex-col justify-between"> | ||
|
@@ -108,8 +130,16 @@ export default /* html */ `<!doctype html> | |
alt="Avatar" | ||
class="w-8 h-8 rounded-full" | ||
/> | ||
<div class="ml-2 bg-gray-800 rounded-lg p-2"> | ||
<p class="text-white">{{ message.text }}</p> | ||
<div | ||
class="ml-2 bg-gray-800 rounded-lg p-2" | ||
style="max-width: 33.33vw" | ||
> | ||
<p | ||
v-if="message.formattedText" | ||
class="overflow-x-scroll" | ||
v-html="message.formattedText" | ||
></p> | ||
<p v-else class="text-white">{{ message.text }}</p> | ||
</div> | ||
</div> | ||
<p class="text-gray-500 mt-1 text-xs ml-10">{{ message.date }}</p> | ||
|
@@ -155,4 +185,5 @@ export default /* html */ `<!doctype html> | |
</div> | ||
</main> | ||
</body> | ||
</html>`; | ||
</html> | ||
`.trim(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters