mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-13 06:10:01 -04:00
clean unneeded stuff
This commit is contained in:
commit
f97e3b247f
3 changed files with 47 additions and 44 deletions
|
@ -5,8 +5,8 @@ import {
|
||||||
GetRequestHeadersCallback,
|
GetRequestHeadersCallback,
|
||||||
MetaCallback,
|
MetaCallback,
|
||||||
ReadyStateCallback,
|
ReadyStateCallback,
|
||||||
|
WebSocketFields,
|
||||||
WebSocketImpl,
|
WebSocketImpl,
|
||||||
WebSocketFields
|
|
||||||
} from "bare-client-custom";
|
} from "bare-client-custom";
|
||||||
import { Connection } from "./Connection";
|
import { Connection } from "./Connection";
|
||||||
|
|
||||||
|
@ -67,6 +67,7 @@ export class AdriftBareClient extends Client {
|
||||||
headers,
|
headers,
|
||||||
}) as BareResponse;
|
}) as BareResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
remote: URL,
|
remote: URL,
|
||||||
protocols: string[],
|
protocols: string[],
|
||||||
|
@ -75,34 +76,36 @@ export class AdriftBareClient extends Client {
|
||||||
onReadyState: ReadyStateCallback,
|
onReadyState: ReadyStateCallback,
|
||||||
webSocketImpl: WebSocketImpl
|
webSocketImpl: WebSocketImpl
|
||||||
): WebSocket {
|
): WebSocket {
|
||||||
const ws = new WebSocket("ws:null");
|
const ws = new webSocketImpl("ws:null", protocols);
|
||||||
// this will error. that's okay
|
// this will error. that's okay
|
||||||
|
|
||||||
|
let send = this.connection.wsconnect(
|
||||||
|
remote,
|
||||||
|
() => {
|
||||||
|
onReadyState(WebSocketFields.OPEN);
|
||||||
|
ws.dispatchEvent(new Event("open"));
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
onReadyState(WebSocketFields.CLOSED);
|
||||||
|
ws.dispatchEvent(new Event("close"));
|
||||||
|
|
||||||
let send = this.connection.wsconnect(remote, () => {
|
// what do i do for WebSocketFields.closing?
|
||||||
onReadyState(WebSocketFields.OPEN);
|
},
|
||||||
ws.dispatchEvent(new Event('open'));
|
(data) => {
|
||||||
|
ws.dispatchEvent(
|
||||||
|
new MessageEvent("message", {
|
||||||
|
data,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
}, () => {
|
|
||||||
onReadyState(WebSocketFields.CLOSED);
|
|
||||||
ws.dispatchEvent(new Event('close'));
|
|
||||||
|
|
||||||
// what do i do for WebSocketFields.closing?
|
|
||||||
}, (data) => {
|
|
||||||
ws.dispatchEvent(new MessageEvent("message", {
|
|
||||||
data,
|
|
||||||
// might be other shit here idk
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
(ws as any).__defineGetter__("send", () => (data: any) => {
|
(ws as any).__defineGetter__("send", () => (data: any) => {
|
||||||
send(data);
|
send(data);
|
||||||
});
|
});
|
||||||
(ws as any).__defineSetter__("send", () => { });
|
(ws as any).__defineSetter__("send", () => { });
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return ws;
|
return ws;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,10 @@ import expressWs from "express-ws";
|
||||||
|
|
||||||
import { signInWithEmailAndPassword } from "firebase/auth";
|
import { signInWithEmailAndPassword } from "firebase/auth";
|
||||||
import wrtc from "wrtc";
|
import wrtc from "wrtc";
|
||||||
import { Client } from "./client";
|
|
||||||
|
|
||||||
import { auth } from "firebase-config";
|
import { auth } from "firebase-config";
|
||||||
import { getDatabase, onValue, ref, set } from "firebase/database";
|
import { getDatabase, onValue, ref, set } from "firebase/database";
|
||||||
|
import { AdriftServer } from "./server";
|
||||||
|
|
||||||
const configuration = {
|
const configuration = {
|
||||||
iceServers: [
|
iceServers: [
|
||||||
|
@ -98,20 +98,20 @@ async function answerRtc(data: any, onrespond: (answer: any) => void) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let client: Client;
|
let server: AdriftServer;
|
||||||
|
|
||||||
dataChannel.onopen = () => {
|
dataChannel.onopen = () => {
|
||||||
console.log("opened");
|
console.log("opened");
|
||||||
client = new Client((msg) => dataChannel.send(msg));
|
server = new AdriftServer((msg) => dataChannel.send(msg));
|
||||||
};
|
};
|
||||||
dataChannel.onclose = () => {
|
dataChannel.onclose = () => {
|
||||||
console.log("closed");
|
console.log("closed");
|
||||||
client.onClose();
|
server.onClose();
|
||||||
};
|
};
|
||||||
dataChannel.onmessage = (event) => {
|
dataChannel.onmessage = (event) => {
|
||||||
console.log("messaged");
|
console.log("messaged");
|
||||||
if (event.data instanceof Buffer) {
|
if (event.data instanceof Buffer) {
|
||||||
client.onMsg(bufferToArrayBuffer(event.data));
|
server.onMsg(bufferToArrayBuffer(event.data));
|
||||||
}
|
}
|
||||||
throw new Error("Unexpected datachannel message type");
|
throw new Error("Unexpected datachannel message type");
|
||||||
};
|
};
|
||||||
|
@ -127,7 +127,7 @@ app.post("/connect", (req, res) => {
|
||||||
|
|
||||||
app.ws("/dev-ws", (ws, _req) => {
|
app.ws("/dev-ws", (ws, _req) => {
|
||||||
console.log("ws connect");
|
console.log("ws connect");
|
||||||
const client = new Client((msg) => ws.send(msg));
|
const client = new AdriftServer((msg) => ws.send(msg));
|
||||||
|
|
||||||
ws.on("message", (msg) => {
|
ws.on("message", (msg) => {
|
||||||
if (typeof msg === "string") {
|
if (typeof msg === "string") {
|
||||||
|
|
|
@ -12,7 +12,21 @@ import {
|
||||||
import { Readable } from "stream";
|
import { Readable } from "stream";
|
||||||
import { BareError, bareFetch, options } from "./http";
|
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;
|
send: (msg: ArrayBuffer) => void;
|
||||||
events: EventEmitter;
|
events: EventEmitter;
|
||||||
|
|
||||||
|
@ -58,20 +72,6 @@ export class Client {
|
||||||
return payload;
|
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<{
|
async handleHTTPRequest(payload: HTTPRequestPayload): Promise<{
|
||||||
payload: HTTPResponsePayload;
|
payload: HTTPResponsePayload;
|
||||||
body: AsyncIterable<ArrayBuffer>;
|
body: AsyncIterable<ArrayBuffer>;
|
||||||
|
@ -93,7 +93,7 @@ export class Client {
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof BareError) {
|
if (e instanceof BareError) {
|
||||||
return Client.bareErrorToResponse(e);
|
return bareErrorToResponse(e);
|
||||||
}
|
}
|
||||||
this.events.off("close", onClose);
|
this.events.off("close", onClose);
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -149,7 +149,7 @@ export class Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
async onMsg(msg: ArrayBuffer) {
|
async onMsg(msg: ArrayBuffer) {
|
||||||
const init = Client.parseMsgInit(msg);
|
const init = AdriftServer.parseMsgInit(msg);
|
||||||
if (!init) return;
|
if (!init) return;
|
||||||
const { cursor, seq, op } = init;
|
const { cursor, seq, op } = init;
|
||||||
switch (op) {
|
switch (op) {
|
||||||
|
@ -158,7 +158,7 @@ export class Client {
|
||||||
payload: HTTPResponsePayload;
|
payload: HTTPResponsePayload;
|
||||||
body: AsyncIterable<ArrayBuffer>;
|
body: AsyncIterable<ArrayBuffer>;
|
||||||
};
|
};
|
||||||
const reqPayload = Client.parseHttpReqPayload(msg.slice(cursor));
|
const reqPayload = AdriftServer.parseHttpReqPayload(msg.slice(cursor));
|
||||||
if (!reqPayload) return;
|
if (!reqPayload) return;
|
||||||
try {
|
try {
|
||||||
resp = await this.handleHTTPRequest(reqPayload);
|
resp = await this.handleHTTPRequest(reqPayload);
|
||||||
|
@ -184,7 +184,7 @@ export class Client {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = Client.bareErrorToResponse(bareError);
|
resp = bareErrorToResponse(bareError);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { payload, body } = resp;
|
const { payload, body } = resp;
|
Loading…
Add table
Add a link
Reference in a new issue