mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-13 06:10:01 -04:00
start of server impl
This commit is contained in:
parent
9d09289c5d
commit
636cdbf561
1 changed files with 66 additions and 1 deletions
|
@ -2,6 +2,7 @@ import dotenv from "dotenv";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import expressWs from "express-ws";
|
import expressWs from "express-ws";
|
||||||
import * as wrtc from "wrtc";
|
import * as wrtc from "wrtc";
|
||||||
|
import { C2SRequestTypes } from "../protocol";
|
||||||
|
|
||||||
const configuration = {
|
const configuration = {
|
||||||
iceServers: [
|
iceServers: [
|
||||||
|
@ -90,10 +91,74 @@ app.post("/connect", (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
class Client {
|
||||||
|
send: (msg: Buffer) => void;
|
||||||
|
|
||||||
|
constructor(send) {
|
||||||
|
this.send = send;
|
||||||
|
}
|
||||||
|
|
||||||
|
static parseMsgInit(
|
||||||
|
msg: Buffer
|
||||||
|
): { cursor: number; seq: number; op: number } | undefined {
|
||||||
|
try {
|
||||||
|
let cursor = 0;
|
||||||
|
const seq = msg.readUint16BE(cursor);
|
||||||
|
cursor += 2;
|
||||||
|
const op = msg.readUint8(cursor);
|
||||||
|
cursor += 1;
|
||||||
|
return { cursor, seq, op };
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof RangeError) {
|
||||||
|
// malformed message
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static parseHttpReqPayload(payloadRaw: Buffer) {
|
||||||
|
let payload;
|
||||||
|
try {
|
||||||
|
payload = JSON.parse(payloadRaw.toString());
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof SyntaxError) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
console.log({ payload });
|
||||||
|
}
|
||||||
|
|
||||||
|
onMsg(msg: Buffer) {
|
||||||
|
const init = Client.parseMsgInit(msg);
|
||||||
|
if (!init) return;
|
||||||
|
const { cursor, seq, op } = init;
|
||||||
|
switch (op) {
|
||||||
|
case C2SRequestTypes.HTTPRequest:
|
||||||
|
Client.parseHttpReqPayload(msg.subarray(cursor));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// not implemented
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
app.ws("/dev-ws", (ws, req) => {
|
app.ws("/dev-ws", (ws, req) => {
|
||||||
console.log("ws connect");
|
console.log("ws connect");
|
||||||
|
const client = new Client((msg) => ws.send(msg));
|
||||||
|
|
||||||
ws.on("message", (msg) => {
|
ws.on("message", (msg) => {
|
||||||
console.log({ msg });
|
if (typeof msg === "string") {
|
||||||
|
msg = Buffer.from(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg instanceof Buffer) {
|
||||||
|
client.onMsg(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new Error("Unexpected message type");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue