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:
@@ -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),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user