deno server
This commit is contained in:
157
deno/main.ts
Normal file
157
deno/main.ts
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
function serveFile(filename: string) {
|
||||||
|
console.log(filename)
|
||||||
|
const responseText = Deno.readFileSync("../" + filename);
|
||||||
|
// console.log(responseText)
|
||||||
|
const response = new Response(responseText);
|
||||||
|
|
||||||
|
if (filename.endsWith('.js')) {
|
||||||
|
response.headers.set('content-type', 'application/javascript')
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pingHandler(m:any) {
|
||||||
|
console.log("pong handler", m);
|
||||||
|
|
||||||
|
return '{"type":"pong"}'
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HelloMessage {
|
||||||
|
type: string
|
||||||
|
user_id: string
|
||||||
|
user_name: string
|
||||||
|
peer_id:string
|
||||||
|
peer_name: string
|
||||||
|
known_users:string[]
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const userPeers:Map<string, Set<string>> = new Map();
|
||||||
|
|
||||||
|
function helloHandler(m:HelloMessage) {
|
||||||
|
console.log(`Received hello from peer ${m.peer_id}:${m.peer_name}, user ${m.user_id}:${m.user_name}`);
|
||||||
|
|
||||||
|
|
||||||
|
if (!userPeers.has(m.user_id)) {
|
||||||
|
userPeers.set(m.user_id, new Set());
|
||||||
|
}
|
||||||
|
userPeers.get(m.user_id)?.add(m.peer_id);
|
||||||
|
|
||||||
|
for (const knownUserID of m.known_users) {
|
||||||
|
console.log(`Adding user ${knownUserID} from peer ${[m.peer_id]}`);
|
||||||
|
if (!userPeers.get(knownUserID)) {
|
||||||
|
userPeers.set(knownUserID, new Set());
|
||||||
|
}
|
||||||
|
|
||||||
|
userPeers.get(knownUserID)?.add(m.peer_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
const peersToReturn = JSON.stringify(userPeers);
|
||||||
|
console.log(peersToReturn);
|
||||||
|
|
||||||
|
return JSON.stringify({type:'hello', userPeers: peersToReturn});
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageDispatch:Map<string, (event:Record<string, never>)=>string> = new Map();
|
||||||
|
|
||||||
|
function connectWebsocket(request: Request) {
|
||||||
|
if (request.headers.get("upgrade") != "websocket") {
|
||||||
|
return new Response(null, { status: 501 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { socket, response } = Deno.upgradeWebSocket(request);
|
||||||
|
socket.addEventListener("open", () => {
|
||||||
|
console.log("a client connected!");
|
||||||
|
});
|
||||||
|
socket.addEventListener("message", (event) => {
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
let message:any;
|
||||||
|
try {
|
||||||
|
message = JSON.parse(event.data);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("socket.message: ", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(message);
|
||||||
|
const dispatchHandler = messageDispatch.get(message.type)
|
||||||
|
if (!dispatchHandler) {
|
||||||
|
console.log("Got message I don't understand: ", event.data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = dispatchHandler(message);
|
||||||
|
console.log(response);
|
||||||
|
socket.send(response);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
return response;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function handler(request: Request) {
|
||||||
|
|
||||||
|
// console.log(request);
|
||||||
|
|
||||||
|
const url = new URL(request.url);
|
||||||
|
|
||||||
|
if (url.pathname === "/ws") {
|
||||||
|
return connectWebsocket(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.pathname === "/") {
|
||||||
|
return serveFile("/static/index.html")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.pathname === "/sw.js") {
|
||||||
|
return serveFile("static/sw.js")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.pathname === "/robots).txt") {
|
||||||
|
return serveFile("static/robots.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.pathname === "/favicon.ico") {
|
||||||
|
return serveFile("static/favicon.ico")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (url.pathname.includes("/static/")) {
|
||||||
|
return serveFile(url.pathname)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if strings.Contains(r.URL.Path, "/static/") {
|
||||||
|
// log.Print("Serving static")
|
||||||
|
|
||||||
|
// path := filepath.Join(root, r.URL.Path)
|
||||||
|
// info, err := os.Stat(path)
|
||||||
|
// if err != nil || info.IsDir() {
|
||||||
|
// log.Printf("404 File not found/dir")
|
||||||
|
// http.NotFound(w, r)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// log.Printf("Serving")
|
||||||
|
// h.ServeHTTP(w, r)
|
||||||
|
// return
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
return new Response("Hello, World!");
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
|
||||||
|
messageDispatch.set('ping', pingHandler);
|
||||||
|
messageDispatch.set('hello', helloHandler);
|
||||||
|
|
||||||
|
Deno.serve({
|
||||||
|
port: 6789,
|
||||||
|
cert: Deno.readTextFileSync("/etc/letsencrypt/live/ddlion.net/fullchain.pem"),
|
||||||
|
key: Deno.readTextFileSync("/etc/letsencrypt/live/ddlion.net/privkey.pem"),
|
||||||
|
}, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
2
main.go
2
main.go
@@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
type PeerSet struct {
|
type PeerSet struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
m map[string]struct{}
|
M map[string]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var userPeersMutex sync.Mutex = sync.Mutex{}
|
var userPeersMutex sync.Mutex = sync.Mutex{}
|
||||||
|
|||||||
@@ -1006,6 +1006,11 @@ class App {
|
|||||||
if ((performance as any)?.memory) {
|
if ((performance as any)?.memory) {
|
||||||
log(`memory used: ${((performance as any).memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`)
|
log(`memory used: ${((performance as any).memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (navigator?.storage) {
|
||||||
|
// let storageUsed = (await navigator?.storage?.estimate())?.usage/1024/1024
|
||||||
|
// }
|
||||||
|
|
||||||
// if (urlParams.get("sw") === "true") {
|
// if (urlParams.get("sw") === "true") {
|
||||||
registration = await this.registerServiceWorker();
|
registration = await this.registerServiceWorker();
|
||||||
// }
|
// }
|
||||||
|
|||||||
@@ -773,6 +773,9 @@ class App {
|
|||||||
if (performance?.memory) {
|
if (performance?.memory) {
|
||||||
log(`memory used: ${(performance.memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`);
|
log(`memory used: ${(performance.memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`);
|
||||||
}
|
}
|
||||||
|
// if (navigator?.storage) {
|
||||||
|
// let storageUsed = (await navigator?.storage?.estimate())?.usage/1024/1024
|
||||||
|
// }
|
||||||
// if (urlParams.get("sw") === "true") {
|
// if (urlParams.get("sw") === "true") {
|
||||||
registration = await this.registerServiceWorker();
|
registration = await this.registerServiceWorker();
|
||||||
// }
|
// }
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user