mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-12 13:50:01 -04:00
Client -> AdriftServer
This commit is contained in:
parent
f6ad470a69
commit
bb6eb218d0
2 changed files with 25 additions and 25 deletions
|
@ -4,10 +4,10 @@ import expressWs from "express-ws";
|
|||
|
||||
import { signInWithEmailAndPassword } from "firebase/auth";
|
||||
import wrtc from "wrtc";
|
||||
import { Client } from "./client";
|
||||
|
||||
import { auth } from "firebase-config";
|
||||
import { getDatabase, onValue, ref, set } from "firebase/database";
|
||||
import { AdriftServer } from "./server";
|
||||
|
||||
const configuration = {
|
||||
iceServers: [
|
||||
|
@ -98,20 +98,20 @@ async function answerRtc(data: any, onrespond: (answer: any) => void) {
|
|||
}
|
||||
});
|
||||
|
||||
let client: Client;
|
||||
let server: AdriftServer;
|
||||
|
||||
dataChannel.onopen = () => {
|
||||
console.log("opened");
|
||||
client = new Client((msg) => dataChannel.send(msg));
|
||||
server = new AdriftServer((msg) => dataChannel.send(msg));
|
||||
};
|
||||
dataChannel.onclose = () => {
|
||||
console.log("closed");
|
||||
client.onClose();
|
||||
server.onClose();
|
||||
};
|
||||
dataChannel.onmessage = (event) => {
|
||||
console.log("messaged");
|
||||
if (event.data instanceof Buffer) {
|
||||
client.onMsg(bufferToArrayBuffer(event.data));
|
||||
server.onMsg(bufferToArrayBuffer(event.data));
|
||||
}
|
||||
throw new Error("Unexpected datachannel message type");
|
||||
};
|
||||
|
@ -127,7 +127,7 @@ app.post("/connect", (req, res) => {
|
|||
|
||||
app.ws("/dev-ws", (ws, _req) => {
|
||||
console.log("ws connect");
|
||||
const client = new Client((msg) => ws.send(msg));
|
||||
const client = new AdriftServer((msg) => ws.send(msg));
|
||||
|
||||
ws.on("message", (msg) => {
|
||||
if (typeof msg === "string") {
|
||||
|
|
|
@ -12,7 +12,21 @@ import {
|
|||
import { Readable } from "stream";
|
||||
import { BareError, bareFetch, options } from "./http";
|
||||
|
||||
export class Client {
|
||||
function bareErrorToResponse(e: BareError): {
|
||||
payload: HTTPResponsePayload;
|
||||
body: AsyncIterable<ArrayBuffer>;
|
||||
} {
|
||||
return {
|
||||
payload: {
|
||||
status: e.status,
|
||||
statusText: STATUS_CODES[e.status] || "",
|
||||
headers: {},
|
||||
},
|
||||
body: Readable.from(JSON.stringify(e.body)),
|
||||
};
|
||||
}
|
||||
|
||||
export class AdriftServer {
|
||||
send: (msg: ArrayBuffer) => void;
|
||||
events: EventEmitter;
|
||||
|
||||
|
@ -58,20 +72,6 @@ export class Client {
|
|||
return payload;
|
||||
}
|
||||
|
||||
static bareErrorToResponse(e: BareError): {
|
||||
payload: HTTPResponsePayload;
|
||||
body: AsyncIterable<ArrayBuffer>;
|
||||
} {
|
||||
return {
|
||||
payload: {
|
||||
status: e.status,
|
||||
statusText: STATUS_CODES[e.status] || "",
|
||||
headers: {},
|
||||
},
|
||||
body: Readable.from(JSON.stringify(e.body)),
|
||||
};
|
||||
}
|
||||
|
||||
async handleHTTPRequest(payload: HTTPRequestPayload): Promise<{
|
||||
payload: HTTPResponsePayload;
|
||||
body: AsyncIterable<ArrayBuffer>;
|
||||
|
@ -93,7 +93,7 @@ export class Client {
|
|||
);
|
||||
} catch (e) {
|
||||
if (e instanceof BareError) {
|
||||
return Client.bareErrorToResponse(e);
|
||||
return bareErrorToResponse(e);
|
||||
}
|
||||
this.events.off("close", onClose);
|
||||
throw e;
|
||||
|
@ -149,7 +149,7 @@ export class Client {
|
|||
}
|
||||
|
||||
async onMsg(msg: ArrayBuffer) {
|
||||
const init = Client.parseMsgInit(msg);
|
||||
const init = AdriftServer.parseMsgInit(msg);
|
||||
if (!init) return;
|
||||
const { cursor, seq, op } = init;
|
||||
switch (op) {
|
||||
|
@ -158,7 +158,7 @@ export class Client {
|
|||
payload: HTTPResponsePayload;
|
||||
body: AsyncIterable<ArrayBuffer>;
|
||||
};
|
||||
const reqPayload = Client.parseHttpReqPayload(msg.slice(cursor));
|
||||
const reqPayload = AdriftServer.parseHttpReqPayload(msg.slice(cursor));
|
||||
if (!reqPayload) return;
|
||||
try {
|
||||
resp = await this.handleHTTPRequest(reqPayload);
|
||||
|
@ -184,7 +184,7 @@ export class Client {
|
|||
});
|
||||
}
|
||||
|
||||
resp = Client.bareErrorToResponse(bareError);
|
||||
resp = bareErrorToResponse(bareError);
|
||||
}
|
||||
|
||||
const { payload, body } = resp;
|
Loading…
Add table
Add a link
Reference in a new issue