Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | import WebSocket from 'ws';
import {RxPersistentCaller} from '@jsonjoy.com/rpc-calls/lib/caller/RxPersistentCaller';
import {FetchCaller} from '@jsonjoy.com/rpc-calls/lib/caller/FetchCaller';
import {WebSocketChannel} from '@jsonjoy.com/channel/lib/WebSocketChannel';
import {RpcMessageFormat} from '@jsonjoy.com/rpc-codec-base/lib/constants';
import type {RpcCodec} from '@jsonjoy.com/rpc-codec/lib/RpcCodec';
import type {BinBatchCodec} from '@jsonjoy.com/rpc-codec-base';
const secure = true;
const host = 'pub-1-api.jsonjoy.org';
// const host = '127.0.0.1:8080';
const rpcCodecToBinBatchCodec = (rpcCodec: RpcCodec<any>): BinBatchCodec<any> => ({
id: rpcCodec.specifier(),
format: rpcCodec.msg.format ?? RpcMessageFormat.Compact,
toChunk: (messages: any[]) => rpcCodec.encode(messages, rpcCodec.req),
fromChunk: (chunk: Uint8Array) => rpcCodec.decode(chunk, rpcCodec.res),
});
export const setupDemoServerPersistentClient = (codec: RpcCodec<any>) => {
const url = `ws${secure ? 's' : ''}://${host}/rx`;
const caller = new RxPersistentCaller({
codec,
physical: {
newChannel: () =>
new WebSocketChannel({
newSocket: () => new WebSocket(url, [codec.specifier()]) as any,
}),
},
});
caller.start();
const call = caller.call.bind(caller);
const call$ = caller.call$.bind(caller);
const stop = async () => void caller.stop();
return {caller, call, call$, stop};
};
export const setupDemoServerFetchClient = (codec: RpcCodec<any>) => {
const url = `http${secure ? 's' : ''}://${host}/rx`;
const caller = new FetchCaller({
url,
codec: rpcCodecToBinBatchCodec(codec),
headers: {
'Content-Type': `application/x.${codec.specifier()}`,
},
});
const call = caller.call.bind(caller);
const call$ = caller.call$.bind(caller);
const stop = async () => void caller.stop();
return {caller, call, call$, stop};
};
|