mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-12 22:00:02 -04:00
connect to tracker inside server
This commit is contained in:
parent
a839d0fe51
commit
5cef78d47c
4 changed files with 27 additions and 11 deletions
|
@ -9,7 +9,7 @@ import {
|
|||
import { Agent as HTTPSAgent, request as httpsRequest } from "https";
|
||||
import fuck from "ipaddr.js";
|
||||
import { HTTPRequestPayload } from "protocol";
|
||||
import { Readable } from "stream";
|
||||
// import { Readable } from "stream";
|
||||
const { isValid, parse } = fuck;
|
||||
|
||||
export interface BareErrorBody {
|
||||
|
@ -172,8 +172,8 @@ export async function bareFetch(
|
|||
});
|
||||
else throw new RangeError(`Unsupported protocol: '${remote.protocol}'`);
|
||||
|
||||
if (request.body) Readable.from([request.body]).pipe(outgoing);
|
||||
else outgoing.end();
|
||||
// if (request.body) Readable.from([request.body]).pipe(outgoing);
|
||||
// else outgoing.end();
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
outgoing.on("response", (response: IncomingMessage) => {
|
||||
|
|
|
@ -9,6 +9,9 @@ import { auth } from "firebase-config";
|
|||
import { getDatabase, onValue, ref, set } from "firebase/database";
|
||||
import { AdriftServer } from "./server";
|
||||
|
||||
import { WebSocket } from "isomorphic-ws";
|
||||
|
||||
|
||||
const configuration = {
|
||||
iceServers: [
|
||||
{
|
||||
|
@ -161,11 +164,7 @@ async function connectFirebase() {
|
|||
|
||||
if (str) {
|
||||
let data = JSON.parse(str);
|
||||
console.log(data);
|
||||
console.log(data.offer);
|
||||
console.log(data.localCandidates);
|
||||
if (data && data.offer && data.localCandidates) {
|
||||
console.log("answerng");
|
||||
answerRtc(data, (answer) => {
|
||||
console.log("answering");
|
||||
set(peer, JSON.stringify(answer));
|
||||
|
@ -176,4 +175,18 @@ async function connectFirebase() {
|
|||
}
|
||||
connectFirebase();
|
||||
|
||||
let tracker = new WebSocket("ws://localhost:17776/join");
|
||||
tracker.on("message", (str: string) => {
|
||||
if (!str) return;
|
||||
let data = JSON.parse(str);
|
||||
if (!(data && data.offer && data.localCandidates)) return;
|
||||
console.log("got offer");
|
||||
|
||||
answerRtc(data, (answer) => {
|
||||
console.log("have an answer");
|
||||
tracker.send(JSON.stringify(answer));
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
app.listen(3000, () => console.log("listening"));
|
||||
|
|
|
@ -199,7 +199,7 @@ export class AdriftServer {
|
|||
if (!init) return;
|
||||
const { cursor, seq, op } = init;
|
||||
switch (op) {
|
||||
case C2SRequestTypes.HTTPRequest: {
|
||||
case C2SRequestTypes.HTTPRequestChunk: {
|
||||
let resp: {
|
||||
payload: HTTPResponsePayload;
|
||||
body: AsyncIterable<ArrayBuffer>;
|
||||
|
|
|
@ -49,10 +49,16 @@ reff.on("value", snapshot => {
|
|||
if (members.length < 1) {
|
||||
db.ref(`/swarm/${key}`).set(JSON.stringify({ error: "no swarm members found" }));
|
||||
console.error("no swarm members!");
|
||||
return;
|
||||
}
|
||||
|
||||
let selectedmember = members[Math.floor(Math.random() * members.length)];
|
||||
|
||||
selectedmember.once("message", (answer) => {
|
||||
db.ref(`/swarm/${key}`).set(answer);
|
||||
});
|
||||
selectedmember.send(offer);
|
||||
|
||||
}
|
||||
|
||||
ids = ids.concat(newkeys);
|
||||
|
@ -63,9 +69,6 @@ app.ws("/join", (ws, _req) => {
|
|||
|
||||
console.log("ws connect");
|
||||
members.push(ws);
|
||||
ws.on("message", (msg) => {
|
||||
|
||||
});
|
||||
|
||||
ws.onclose = () => {
|
||||
members.filter(member => member != ws);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue