Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowCait committed Dec 28, 2024
1 parent e357eb2 commit d400446
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ module.exports.publishEvent = (relays, event) => {

let timeoutId;
return new Promise((resolve, reject) => {
const wss = relays.map(relay => new WebSocket(relay));
const wss = relays.map(relay => {
try {
const ws = new WebSocket(relay);
return ws;
} catch (error) {
console.warn('[connection error]', relay, error);
}
}).filter(ws => ws !== undefined);
if (wss.length === 0) {
reject();
}
const messages = new Map();
const close = () => {
if (timeoutId !== undefined) {
Expand Down Expand Up @@ -61,6 +71,9 @@ module.exports.publishEvent = (relays, event) => {
}, 3000);
for (const ws of wss) {
console.time(ws.url);
ws.onerror = (error) => {
console.warn('[error]', ws.url, error);
};
ws.onopen = () => {
console.log('[open]', ws.url);
ws.send(JSON.stringify(['EVENT', event]));
Expand Down

0 comments on commit d400446

Please sign in to comment.