mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-13 06:10:01 -04:00
split server into dev and cli
This commit is contained in:
parent
9ec17943c2
commit
ee8ae5a3f8
5 changed files with 96 additions and 82 deletions
|
@ -0,0 +1,62 @@
|
|||
import dotenv from "dotenv";
|
||||
import express from "express";
|
||||
import expressWs from "express-ws";
|
||||
|
||||
import { AdriftServer, connectTracker } from "./server";
|
||||
|
||||
import WebSocket from "isomorphic-ws";
|
||||
import { answerRtc, bufferToArrayBuffer, connect } from "./rtc";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
||||
const app = express() as unknown as expressWs.Application;
|
||||
expressWs(app);
|
||||
|
||||
app.use(express.json());
|
||||
app.use((_req, res, next) => {
|
||||
res.header("x-robots-tag", "noindex");
|
||||
res.header("access-control-allow-headers", "*");
|
||||
res.header("access-control-allow-origin", "*");
|
||||
res.header("access-control-allow-methods", "*");
|
||||
res.header("access-control-expose-headers", "*");
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
app.post("/connect", (req, res) => {
|
||||
const data = req.body;
|
||||
answerRtc(data, (d) => {
|
||||
res.json(d);
|
||||
});
|
||||
});
|
||||
|
||||
app.ws("/dev-ws", (ws, _req) => {
|
||||
console.log("ws connect");
|
||||
const client = new AdriftServer((msg) => ws.send(msg));
|
||||
|
||||
ws.on("message", (msg) => {
|
||||
if (typeof msg === "string") {
|
||||
msg = Buffer.from(msg);
|
||||
}
|
||||
|
||||
if (msg instanceof Buffer) {
|
||||
client.onMsg(bufferToArrayBuffer(msg));
|
||||
return;
|
||||
}
|
||||
throw new Error("Unexpected message type");
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
let tracker = new WebSocket("ws://localhost:17776/join");
|
||||
tracker.onerror = console.error;
|
||||
connectTracker(tracker);
|
||||
} catch (_) {
|
||||
|
||||
}
|
||||
|
||||
app.listen(3000, () => console.log("listening"));
|
Loading…
Add table
Add a link
Reference in a new issue