Skip to content

Instantly share code, notes, and snippets.

@imksoo
Created July 8, 2023 00:46
Show Gist options
  • Save imksoo/b4cf1d933807aa4ca676f3032c8b3dcd to your computer and use it in GitHub Desktop.
Save imksoo/b4cf1d933807aa4ca676f3032c8b3dcd to your computer and use it in GitHub Desktop.
const fetch_count = 16;
const srcRelay = 'ws://localhost:8080'
const dstRelay = 'ws://localhost:8888'
let srcSocket;
let dstSocket;
let until = (new Date()) / 1000;
if (typeof (WebSocket) === 'undefined') {
const WebSocket = require('websocket').w3cwebsocket
srcSocket = new WebSocket(srcRelay)
dstSocket = new WebSocket(dstRelay)
} else {
srcSocket = new WebSocket(srcRelay)
dstSocket = new WebSocket(dstRelay)
}
srcSocket.onopen = () => {
console.log('Destination Websocket is open.')
dstSocket.onopen = () => {
console.log('Source Websocket is open.')
srcSocket.send(JSON.stringify(['REQ', crypto.randomUUID(), {
limit: fetch_count
}]))
}
}
dstSocket.onmessage = (message) => {
console.log('Destination Websocket: ', message.data);
}
srcSocket.onmessage = (message) => {
const event = JSON.parse(message.data);
if (event[0] === 'EOSE') {
srcSocket.send(JSON.stringify(["CLOSE", event[1]]));
console.log(`until = ${until}`)
const req = ['REQ', crypto.randomUUID(), {
until: until,
limit: fetch_count
}];
console.log(JSON.stringify(req));
setTimeout(() => {
srcSocket.send(JSON.stringify(req));
}, 200);
} else if (event[0] === 'EVENT' ) {
const e = event[2]
if ( e.created_at < until ) {
until = e.created_at;
}
console.log(JSON.stringify(e.id))
dstSocket.send(JSON.stringify(['EVENT', e]))
}
}
dstSocket.onclose = () => {
console.log('Destination Websocket is closed.')
}
dstSocket.onerror = (e) => {
console.log('Destination Websocket encountered error: ', e)
}
srcSocket.onclose = () => {
console.log('Source Websocket is closed.')
}
srcSocket.onerror = (e) => {
console.log('Source Websocket encountered error: ', e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment