-
Notifications
You must be signed in to change notification settings - Fork 17
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
14 changed files
with
440 additions
and
150 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
ignore-workspace-root-check=true |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createApp, createRouter, eventHandler } from "h3"; | ||
import { defineWebSocketHooks } from "crossws"; | ||
|
||
const router = createRouter(); | ||
export const app = createApp().use(router); | ||
|
||
router.get( | ||
"/", | ||
eventHandler(() => | ||
import("./index.html").then((r) => r.html({ name: "h3" })), | ||
), | ||
); | ||
|
||
// Listhen automatically sets up ws integration! | ||
export const websocket = defineWebSocketHooks({ | ||
open(peer) { | ||
console.log("[ws] open", peer); | ||
}, | ||
|
||
message(peer, message) { | ||
console.log("[ws] message", peer, message); | ||
if (message.text().includes("ping")) { | ||
peer.send("pong"); | ||
} | ||
}, | ||
|
||
close(peer, event) { | ||
console.log("[ws] close", peer, event); | ||
}, | ||
|
||
error(peer, error) { | ||
console.log("[ws] error", peer, error); | ||
}, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export const html = ({ name }) => /* html */ `<!doctype html> | ||
<head> | ||
<title>CrossWS Test Page</title> | ||
</head> | ||
<body> | ||
<h1>CrossWS Test Page (${name || "?"})</h1> | ||
<button onclick="sendPing()">Send Ping</button> | ||
<button onclick="connect()">Reconnect</button> | ||
<button onclick="clearLogs()">Clear</button> | ||
<pre id="logs"></pre> | ||
<script type="module"> | ||
const url = "ws://" + location.host + "/_ws"; | ||
const logsEl = document.querySelector("#logs"); | ||
let lastTime = performance.now(); | ||
const log = (...args) => { | ||
const now = performance.now(); | ||
const time = Math.round((now - lastTime) * 1000) / 1000; | ||
lastTime = now; | ||
console.log("[ws]", ...args); | ||
logsEl.innerText += "\\n (" + String(time).padStart(4, ' ') + "ms) " + args.join(" "); | ||
}; | ||
let ws | ||
globalThis.connect = async () => { | ||
if (ws) { | ||
log("Closing..."); | ||
ws.close(); | ||
} | ||
log("Connecting to", url, "..."); | ||
ws = new WebSocket(url); | ||
ws.addEventListener("message", (event) => { | ||
log("Message from server:", event.data); | ||
}); | ||
log("Waiting for connection..."); | ||
await new Promise((resolve) => ws.addEventListener("open", resolve)); | ||
} | ||
globalThis.clearLogs = () => { | ||
logsEl.innerText = '' | ||
} | ||
globalThis.sendPing = () => { | ||
log("Sending ping..."); | ||
ws.send("ping"); | ||
} | ||
await connect(); | ||
sendPing() | ||
</script> | ||
</body> | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "crossws-examples-h3", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "listhen --ws ./app.ts", | ||
"dev": "listhen --ws -w ./app.ts" | ||
}, | ||
"dependencies": { | ||
"crossws": "latest", | ||
"h3": "latest", | ||
"listhen": "latest" | ||
} | ||
} |
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.