2019-01-12 03:33:44 +08:00
|
|
|
import { Emitter } from "@coder/events";
|
|
|
|
import { Client } from "../src/browser/client";
|
2019-01-16 02:36:09 +08:00
|
|
|
import { Server, ServerOptions } from "../src/node/server";
|
2019-01-12 03:33:44 +08:00
|
|
|
|
2019-01-16 02:36:09 +08:00
|
|
|
export const createClient = (serverOptions?: ServerOptions): Client => {
|
2019-01-12 03:58:31 +08:00
|
|
|
const s2c = new Emitter<Uint8Array | Buffer>();
|
|
|
|
const c2s = new Emitter<Uint8Array | Buffer>();
|
2019-01-12 03:33:44 +08:00
|
|
|
|
2019-01-16 02:36:09 +08:00
|
|
|
// tslint:disable-next-line
|
2019-01-12 03:58:31 +08:00
|
|
|
new Server({
|
2019-01-16 02:36:09 +08:00
|
|
|
close: (): void => undefined,
|
|
|
|
onClose: (): void => undefined,
|
|
|
|
onMessage: (cb): void => {
|
2019-01-12 03:58:31 +08:00
|
|
|
c2s.event((d) => cb(d));
|
|
|
|
},
|
2019-01-16 02:36:09 +08:00
|
|
|
send: (data): NodeJS.Timer => setTimeout(() => s2c.emit(data), 0),
|
|
|
|
}, serverOptions);
|
2019-01-12 03:33:44 +08:00
|
|
|
|
2019-01-12 03:58:31 +08:00
|
|
|
const client = new Client({
|
2019-01-16 02:36:09 +08:00
|
|
|
close: (): void => undefined,
|
|
|
|
onClose: (): void => undefined,
|
|
|
|
onMessage: (cb): void => {
|
2019-01-12 03:58:31 +08:00
|
|
|
s2c.event((d) => cb(d));
|
|
|
|
},
|
2019-01-16 02:36:09 +08:00
|
|
|
send: (data): NodeJS.Timer => setTimeout(() => c2s.emit(data), 0),
|
2019-01-12 03:58:31 +08:00
|
|
|
});
|
2019-01-12 03:33:44 +08:00
|
|
|
|
2019-01-12 03:58:31 +08:00
|
|
|
return client;
|
2019-01-12 03:33:44 +08:00
|
|
|
};
|