mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-13 06:10:01 -04:00
add firebase login and offering to the client
This commit is contained in:
parent
d1bef00d93
commit
7315350858
4 changed files with 41 additions and 70 deletions
|
@ -1,69 +0,0 @@
|
|||
import {
|
||||
BareClient,
|
||||
registerRemoteListener,
|
||||
setBareClientImplementation,
|
||||
} from "bare-client-custom";
|
||||
import { RTCTransport } from "./RTCTransport";
|
||||
//import "./firebase";
|
||||
|
||||
|
||||
import { AdriftBareClient } from "./AdriftClient";
|
||||
import Connection from "./Connection";
|
||||
import { DevWsTransport } from "./DevWsTransport";
|
||||
|
||||
const rtcEnable = false;
|
||||
|
||||
let rtc;
|
||||
let connection;
|
||||
if (rtcEnable) {
|
||||
rtc = new RTCTransport(
|
||||
console.log,
|
||||
() => {
|
||||
// rtc.dataChannel.send("test message");
|
||||
// let client = new AdriftBareClient;
|
||||
// setBareClientImplementation(client);
|
||||
//
|
||||
},
|
||||
console.log,
|
||||
console.log,
|
||||
console.log
|
||||
);
|
||||
connection = new Connection(rtc);
|
||||
} else {
|
||||
connection = new Connection(
|
||||
new DevWsTransport(
|
||||
() => console.log("onopen"),
|
||||
() => console.log("onclose")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
window["co"] = connection;
|
||||
// connection.httprequest({ a: 1, b: 2 });
|
||||
|
||||
let bare = new AdriftBareClient(connection);
|
||||
setBareClientImplementation(bare);
|
||||
registerRemoteListener();
|
||||
|
||||
// if (import.meta.env.VITE_APP_ENV === 'development') {
|
||||
|
||||
// if (rtcEnable) {
|
||||
// let offer = await this.rtc.createOffer();
|
||||
// console.log("offer created", offer);
|
||||
// console.log(JSON.stringify(offer));
|
||||
//
|
||||
// const r = await fetch("http://localhost:3000/connect", {
|
||||
// method: "POST",
|
||||
// headers: { "Content-Type": "application/json" },
|
||||
// body: JSON.stringify(offer),
|
||||
// });
|
||||
// if (r.status != 200) {
|
||||
// throw new Error("connect: " + r.status + " " + r.statusText);
|
||||
// }
|
||||
// const { answer, candidates } = await r.json();
|
||||
// await this.rtc.answer(answer, candidates);
|
||||
// alert("connected");
|
||||
// } else {
|
||||
// window["bare"].fetch("https://httpbin.org/get");
|
||||
// }
|
||||
//
|
|
@ -1,6 +1,7 @@
|
|||
import "../firebase-config";
|
||||
import { getDatabase, ref, onValue, set } from "firebase/database";
|
||||
|
||||
|
||||
const db = getDatabase();
|
||||
console.log(db);
|
||||
let reff = ref(db, "/peers/demo");
|
||||
|
|
|
@ -16,7 +16,10 @@ const firebaseConfig = {
|
|||
messagingSenderId: "175846512414",
|
||||
appId: "1:175846512414:web:5c6e06d231ab58e9029b0f",
|
||||
measurementId: "G-L0P2EF6Q72"
|
||||
|
||||
};
|
||||
console.warn("firebase is initializing");
|
||||
|
||||
// Initialize Firebase
|
||||
export const app = initializeApp(firebaseConfig);
|
||||
export const auth = getAuth(app);
|
||||
|
|
|
@ -4,11 +4,15 @@
|
|||
import { DevWsTransport } from "../client/DevWsTransport";
|
||||
import { RTCTransport } from "../client/RTCTransport";
|
||||
import type Transport from "../client/Transport";
|
||||
// note: even though we import firebase, due to the tree shaking, it will only run if we use "auth" so if ADRIFT_DEV is set it won't import
|
||||
import { auth } from "../firebase-config";
|
||||
import {
|
||||
BareClient,
|
||||
registerRemoteListener,
|
||||
setBareClientImplementation,
|
||||
} from "bare-client-custom";
|
||||
import { signInWithEmailAndPassword } from "firebase/auth";
|
||||
import { getDatabase, onValue, ref, set } from "firebase/database";
|
||||
|
||||
let transport: Transport;
|
||||
|
||||
|
@ -50,6 +54,38 @@
|
|||
console.log("registering bare-client-custom");
|
||||
registerRemoteListener();
|
||||
}
|
||||
async function connectFirebase() {
|
||||
if (!rtctransport) return;
|
||||
|
||||
let creds = await signInWithEmailAndPassword(
|
||||
auth,
|
||||
"test@test.com",
|
||||
"123456"
|
||||
);
|
||||
|
||||
const db = getDatabase();
|
||||
let peer = ref(db, `/peers/${creds.user.uid}`);
|
||||
|
||||
let offer = await rtctransport.createOffer();
|
||||
|
||||
set(peer, JSON.stringify(offer));
|
||||
|
||||
onValue(peer, (snapshot) => {
|
||||
const data = snapshot.val();
|
||||
console.log(data);
|
||||
if (data && data.answer && data.candidates) {
|
||||
set(peer, null);
|
||||
const { answer, candidates } = JSON.parse(data);
|
||||
rtctransport?.answer(answer, candidates);
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<h1>testa</h1>
|
||||
<h1>
|
||||
{#if !import.meta.env.VITE_ADRIFT_DEV}
|
||||
<button on:click={connectFirebase}>Connect with firebase </button>
|
||||
{:else}
|
||||
connected to dev server
|
||||
{/if}
|
||||
</h1>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue