mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-12 13:50:01 -04:00
add tracker
This commit is contained in:
parent
0ffb386048
commit
23222b19db
11 changed files with 969 additions and 51 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ dist.js
|
|||
dist.js.map
|
||||
dist.html
|
||||
dist/
|
||||
admin-creds.json
|
||||
|
|
|
@ -10,8 +10,10 @@
|
|||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@types/uuid": "^9.0.2",
|
||||
"bare-client-custom": "file:../bare-client-custom",
|
||||
"firebase": "^10.1.0",
|
||||
"protocol": "workspace:*"
|
||||
"protocol": "workspace:*",
|
||||
"uuid": "^9.0.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ const rtcConf = {
|
|||
};
|
||||
|
||||
|
||||
type Offer = { offer: any; localCandidates: any };
|
||||
export type Offer = { offer: any; localCandidates: any };
|
||||
|
||||
|
||||
export class RTCTransport extends Transport {
|
||||
|
|
40
client/src/SignalFirebase.ts
Normal file
40
client/src/SignalFirebase.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { getDatabase, onValue, ref, set, remove } from "firebase/database";
|
||||
import "../firebase-config";
|
||||
|
||||
import { v4 as uuid } from "uuid";
|
||||
import { Offer } from "./RTCTransport";
|
||||
|
||||
|
||||
export async function signalSwarm(offer: string): Promise<Offer> {
|
||||
let id = uuid();
|
||||
const db = getDatabase();
|
||||
let reff = ref(db, `/swarm/${id}`);
|
||||
|
||||
|
||||
set(reff, offer);
|
||||
|
||||
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
|
||||
onValue(reff, (snapshot) => {
|
||||
const text = snapshot.val();
|
||||
|
||||
if (!text)
|
||||
return;
|
||||
let data = JSON.parse(text);
|
||||
|
||||
if (data.err) {
|
||||
reject(new Error(data.err));
|
||||
return;
|
||||
}
|
||||
if (!(data && data.answer && data.candidates)) return;
|
||||
remove(reff);
|
||||
|
||||
resolve(data);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
import { getDatabase, onValue, ref, set } from "firebase/database";
|
||||
import "../firebase-config";
|
||||
|
||||
const db = getDatabase();
|
||||
console.log(db);
|
||||
let reff = ref(db, "/peers/demo");
|
||||
|
||||
// onValue(reff, (snapshot) => {
|
||||
// const data = snapshot.val();
|
||||
// console.log(data);
|
||||
// });
|
||||
|
||||
var callback: (answer: any, candidates: any[]) => void;
|
||||
|
||||
export function setCallback(call: typeof callback) {
|
||||
callback = call;
|
||||
}
|
||||
export function setOffer(offer: string) {
|
||||
set(reff, offer);
|
||||
}
|
||||
|
||||
onValue(reff, (snapshot) => {
|
||||
const data = snapshot.val();
|
||||
console.log(data);
|
||||
|
||||
if (data && data.answer && data.candidates) {
|
||||
set(reff, null);
|
||||
const { answer, candidates } = JSON.parse(data);
|
||||
callback(answer, candidates);
|
||||
}
|
||||
});
|
|
@ -2,3 +2,4 @@ export { AdriftBareClient } from "./AdriftClient";
|
|||
export { Connection } from "./Connection";
|
||||
export { DevWsTransport } from "./DevWsTransport";
|
||||
export { RTCTransport } from "./RTCTransport";
|
||||
export * as SignalFirebase from "./SignalFirebase";
|
851
pnpm-lock.yaml
generated
851
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
|
@ -8,3 +8,4 @@ packages:
|
|||
- Ultraviolet
|
||||
- Dynamic
|
||||
- bare-client-custom
|
||||
- tracker
|
||||
|
|
30
tracker/package.json
Normal file
30
tracker/package.json
Normal file
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"name": "adrift-tracker",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "src/main.ts",
|
||||
"scripts": {
|
||||
"dev": "nodemon src/main.ts",
|
||||
"start": "ts-node src/main.ts"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dotenv": "^16.3.1",
|
||||
"express-ws": "^5.0.2",
|
||||
"firebase": "^10.1.0",
|
||||
"firebase-admin": "^11.10.1",
|
||||
"firebase-config": "workspace:*",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.1.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/express-ws": "^3.0.1",
|
||||
"@types/node": "^20.4.10",
|
||||
"@types/webrtc": "^0.0.36",
|
||||
"@types/ws": "^8.5.5",
|
||||
"nodemon": "^3.0.1"
|
||||
}
|
||||
}
|
28
tracker/src/main.ts
Normal file
28
tracker/src/main.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import dotenv from "dotenv";
|
||||
import express from "express";
|
||||
import expressWs from "express-ws";
|
||||
|
||||
|
||||
import serviceAccount from "./admin-creds.json";
|
||||
|
||||
import admin, { ServiceAccount } from "firebase-admin";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
||||
const app = express() as unknown as expressWs.Application;
|
||||
expressWs(app);
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount as ServiceAccount),
|
||||
databaseURL: "https://adrift-6c1f6-default-rtdb.firebaseio.com"
|
||||
});
|
||||
|
||||
app.ws("/join", (ws: any, _req: any) => {
|
||||
console.log(ws, _req);
|
||||
});
|
||||
|
||||
app.listen(17776, () => console.log("listening"));
|
31
tracker/tsconfig.json
Normal file
31
tracker/tsconfig.json
Normal file
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2017",
|
||||
"module": "commonjs",
|
||||
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
|
||||
"skipLibCheck": true,
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"moduleResolution": "node",
|
||||
"removeComments": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"resolveJsonModule": true,
|
||||
"baseUrl": "."
|
||||
},
|
||||
"exclude": ["node_modules"],
|
||||
"include": ["./src/**/*.ts"],
|
||||
"ts-node": {
|
||||
"files": true
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue