mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-13 06:10:01 -04:00
refactor to share protocol
This commit is contained in:
parent
51ae252748
commit
2022a4cee8
2 changed files with 23 additions and 28 deletions
|
@ -1,36 +1,17 @@
|
||||||
|
import {
|
||||||
|
C2SRequestType,
|
||||||
|
C2SRequestTypes,
|
||||||
|
S2CRequestType,
|
||||||
|
S2CRequestTypes,
|
||||||
|
} from "../protocol";
|
||||||
import Transport from "./Transport";
|
import Transport from "./Transport";
|
||||||
|
|
||||||
|
|
||||||
type ObjectValues<T> = T[keyof T];
|
|
||||||
const C2SRequestTypes = {
|
|
||||||
HTTPRequest: 0,
|
|
||||||
WSOpen: 1,
|
|
||||||
WSClose: 2,
|
|
||||||
WSSendText: 3,
|
|
||||||
WSSendBinary: 4,
|
|
||||||
} as const;
|
|
||||||
type C2SRequestType = ObjectValues<typeof C2SRequestTypes>
|
|
||||||
|
|
||||||
const S2CRequestTypes = {
|
|
||||||
HTTPResponse: 0,
|
|
||||||
WSOpen: 1,
|
|
||||||
WSDataText: 2,
|
|
||||||
WSDataBinary: 3,
|
|
||||||
} as const;
|
|
||||||
type S2CRequestType = ObjectValues<typeof S2CRequestTypes>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default class Connection {
|
export default class Connection {
|
||||||
|
|
||||||
|
|
||||||
callbacks: Record<number, Function> = {};
|
callbacks: Record<number, Function> = {};
|
||||||
|
|
||||||
counter: number = 0;
|
counter: number = 0;
|
||||||
|
|
||||||
constructor(public transport: Transport) {
|
constructor(public transport: Transport) {
|
||||||
|
|
||||||
transport.ondata = this.ondata.bind(this);
|
transport.ondata = this.ondata.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +42,6 @@ export default class Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
async send(data: ArrayBuffer | Blob, type: C2SRequestType): Promise<number> {
|
async send(data: ArrayBuffer | Blob, type: C2SRequestType): Promise<number> {
|
||||||
|
|
||||||
let requestID = this.counter++;
|
let requestID = this.counter++;
|
||||||
|
|
||||||
let header = new ArrayBuffer(2 + 1);
|
let header = new ArrayBuffer(2 + 1);
|
||||||
|
@ -80,14 +60,12 @@ export default class Connection {
|
||||||
console.log(buf);
|
console.log(buf);
|
||||||
|
|
||||||
return requestID;
|
return requestID;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
httprequest(data: object): Promise<object> {
|
httprequest(data: object): Promise<object> {
|
||||||
let json = JSON.stringify(data);
|
let json = JSON.stringify(data);
|
||||||
|
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
|
|
||||||
let id = this.counter;
|
let id = this.counter;
|
||||||
this.callbacks[id] = resolve;
|
this.callbacks[id] = resolve;
|
||||||
await this.send(new Blob([json]), C2SRequestTypes.HTTPRequest);
|
await this.send(new Blob([json]), C2SRequestTypes.HTTPRequest);
|
||||||
|
|
17
protocol/index.ts
Normal file
17
protocol/index.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
export type ObjectValues<T> = T[keyof T];
|
||||||
|
export const C2SRequestTypes = {
|
||||||
|
HTTPRequest: 0,
|
||||||
|
WSOpen: 1,
|
||||||
|
WSClose: 2,
|
||||||
|
WSSendText: 3,
|
||||||
|
WSSendBinary: 4,
|
||||||
|
} as const;
|
||||||
|
export type C2SRequestType = ObjectValues<typeof C2SRequestTypes>;
|
||||||
|
|
||||||
|
export const S2CRequestTypes = {
|
||||||
|
HTTPResponse: 0,
|
||||||
|
WSOpen: 1,
|
||||||
|
WSDataText: 2,
|
||||||
|
WSDataBinary: 3,
|
||||||
|
} as const;
|
||||||
|
export type S2CRequestType = ObjectValues<typeof S2CRequestTypes>;
|
Loading…
Add table
Add a link
Reference in a new issue