mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-12 22:00:02 -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
|
@ -105,7 +105,7 @@
|
||||||
const { answer, candidates } = data;
|
const { answer, candidates } = data;
|
||||||
connectionState = "Linking to node...";
|
connectionState = "Linking to node...";
|
||||||
await new Promise((r) => {
|
await new Promise((r) => {
|
||||||
setTimeout(r, 500);
|
setTimeout(r, 1000);
|
||||||
});
|
});
|
||||||
rtctransport?.answer(answer, candidates);
|
rtctransport?.answer(answer, candidates);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "src/main.ts",
|
"main": "src/main.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "nodemon src/main.ts",
|
"start": "ts-node src/main.ts",
|
||||||
"start": "ts-node src/main.ts"
|
"dev": "nodemon src/dev.ts"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
|
|
||||||
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
|
|
||||||
|
|
||||||
|
|
||||||
import { getDatabase, onValue, ref, set } from "firebase/database";
|
|
||||||
import { answerRtc } from "./rtc";
|
|
||||||
|
|
||||||
async function connectFirebase() {
|
|
||||||
let creds = await signInWithEmailAndPassword(getAuth(), "test@test.com", "123456");
|
|
||||||
|
|
||||||
const db = getDatabase();
|
|
||||||
let peer = ref(db, `/peers/${creds.user.uid}`);
|
|
||||||
|
|
||||||
set(peer, "");
|
|
||||||
|
|
||||||
onValue(peer, (snapshot) => {
|
|
||||||
const str = snapshot.val();
|
|
||||||
|
|
||||||
if (str) {
|
|
||||||
let data = JSON.parse(str);
|
|
||||||
if (data && data.offer && data.localCandidates) {
|
|
||||||
answerRtc(data, (answer) => {
|
|
||||||
console.log("answering");
|
|
||||||
set(peer, JSON.stringify(answer));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
connectFirebase();
|
|
|
@ -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"));
|
|
@ -1,61 +1,43 @@
|
||||||
import dotenv from "dotenv";
|
|
||||||
import express from "express";
|
|
||||||
import expressWs from "express-ws";
|
|
||||||
|
|
||||||
import { AdriftServer, connectTracker } from "./server";
|
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
|
||||||
|
import { initializeApp } from "firebase/app";
|
||||||
import WebSocket from "isomorphic-ws";
|
|
||||||
import { answerRtc, bufferToArrayBuffer, connect } from "./rtc";
|
|
||||||
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
|
|
||||||
const app = express() as unknown as expressWs.Application;
|
import { getDatabase, onValue, ref, set } from "firebase/database";
|
||||||
expressWs(app);
|
import { answerRtc } from "./rtc";
|
||||||
|
|
||||||
app.use(express.json());
|
import { WebSocket } from "isomorphic-ws";
|
||||||
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();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
import TrackerList from "tracker-list";
|
||||||
|
import { connectTracker } from "./server";
|
||||||
|
|
||||||
|
async function connectFirebase() {
|
||||||
|
let creds = await signInWithEmailAndPassword(getAuth(), "test@test.com", "123456");
|
||||||
|
|
||||||
|
const db = getDatabase();
|
||||||
|
let peer = ref(db, `/peers/${creds.user.uid}`);
|
||||||
|
|
||||||
app.post("/connect", (req, res) => {
|
set(peer, "");
|
||||||
const data = req.body;
|
|
||||||
answerRtc(data, (d) => {
|
|
||||||
res.json(d);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
app.ws("/dev-ws", (ws, _req) => {
|
onValue(peer, (snapshot) => {
|
||||||
console.log("ws connect");
|
const str = snapshot.val();
|
||||||
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");
|
|
||||||
connectTracker(tracker);
|
|
||||||
} catch (_) {
|
|
||||||
|
|
||||||
|
if (str) {
|
||||||
|
let data = JSON.parse(str);
|
||||||
|
if (data && data.offer && data.localCandidates) {
|
||||||
|
answerRtc(data, (answer) => {
|
||||||
|
console.log("answering");
|
||||||
|
set(peer, JSON.stringify(answer));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.listen(3000, () => console.log("listening"));
|
let tracker = TrackerList["us-central-1"];
|
||||||
|
initializeApp(tracker.firebase);
|
||||||
|
|
||||||
|
let trackerws = new WebSocket(tracker.tracker + "/join");
|
||||||
|
connectTracker(trackerws);
|
||||||
|
connectFirebase();
|
||||||
|
console.log("main server, use dev server for dev server things");
|
Loading…
Add table
Add a link
Reference in a new issue