Bugfix Websocket hostname. (#21)

* Add yarn-error.log to gitignore.

* Add ts-node to devDependencies.
Add script to only build tsc.
Add websocket to keywords.

* Update yarn.lock libs.

* FIX websocket endpoint hostname.
FIX corrent import for all examples.
FIX websocket instrucions on readme.
This commit is contained in:
Miguel Medeiros
2021-05-20 12:03:40 -03:00
committed by GitHub
parent 0aa3757217
commit e3068c2d8d
28 changed files with 236 additions and 169 deletions

View File

@@ -2,13 +2,13 @@ import { WsInterface, WsInstance } from '../../interfaces/bitcoin/websockets';
import wsClient from '../../services/ws/client';
import wsServer from '../../services/ws/server';
const defaultWs = 'wss://mempool.space/api/v1/ws';
export const useWebsocket = (hostname: string): WsInstance => {
export const useWebsocket = (hostname?: string): WsInstance => {
const wsEndpoint = `wss://${hostname}/api/v1/ws`;
return {
initClient: ({ options }: WsInterface) =>
wsClient(options, defaultWs, hostname),
wsClient(options, wsEndpoint),
initServer: ({ options }: WsInterface) =>
wsServer(options, defaultWs, hostname),
wsServer(options, wsEndpoint),
};
};

View File

@@ -2,13 +2,13 @@ import { WsInterface, WsInstance } from '../../interfaces/bitcoin/websockets';
import wsClient from '../../services/ws/client';
import wsServer from '../../services/ws/server';
const defaultWs = 'wss://mempool.space/liquid/api/v1/ws';
export const useWebsocket = (hostname: string): WsInstance => {
export const useWebsocket = (hostname?: string): WsInstance => {
const wsEndpoint = `wss://${hostname}/liquid/api/v1/ws`;
return {
initClient: ({ options }: WsInterface) =>
wsClient(options, defaultWs, hostname),
wsClient(options, wsEndpoint),
initServer: ({ options }: WsInterface) =>
wsServer(options, defaultWs, hostname),
wsServer(options, wsEndpoint),
};
};

View File

@@ -1,23 +1,25 @@
const browserWS = (
const serverWS = (
options: string[],
defaultWs: string,
websocketEndpoint?: string
endpoint: string,
): WebSocket => {
const ws = new WebSocket(websocketEndpoint || defaultWs);
ws.addEventListener('open', function open() {
handleMessage(ws, options);
const ws = new WebSocket(endpoint);
ws.addEventListener("open", function open() {
ws.send(JSON.stringify({ action: "want", data: options }));
});
ws.addEventListener("close", async function close() {
await sleep(60000);
serverWS(options, endpoint);
});
return ws;
}
const sleep = (ms: number) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const handleMessage = (ws: WebSocket, options: string[]) => {
ws.send(JSON.stringify({ action: 'init' }));
ws.send(
JSON.stringify({
action: 'want',
data: options,
})
);
};
export default serverWS;
export default browserWS;

View File

@@ -2,23 +2,30 @@ import WebSocket from 'ws';
const serverWS = (
options: string[],
defaultWs: string,
websocketEndpoint?: string
endpoint: string,
): WebSocket => {
const ws = new WebSocket(websocketEndpoint || defaultWs);
ws.on('open', function open() {
handleMessage(ws, options);
const ws = new WebSocket(endpoint);
const interval = setInterval(function ping() {
ws.ping();
}, 30000);
ws.on("open", function open() {
ws.send(JSON.stringify({ action: "want", data: options }));
});
ws.on("close", async function close() {
clearInterval(interval);
ws.terminate();
await sleep(60000);
serverWS(options, endpoint);
});
return ws;
}
const sleep = (ms: number) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const handleMessage = (ws: WebSocket, options: string[]) => {
ws.send(JSON.stringify({ action: 'init' }));
ws.send(
JSON.stringify({
action: 'want',
data: options,
})
);
};
export default serverWS;