fix: only one ws connection open

This commit is contained in:
Tiago Vasconcelos 2023-03-20 10:01:21 +00:00
parent aa8e9eea9a
commit e6f7dc85e0
2 changed files with 39 additions and 59 deletions

View file

@ -129,7 +129,7 @@ async function chatDialog(path) {
this.nostrMessages = Array.from(messagesMap.values()) this.nostrMessages = Array.from(messagesMap.values())
} }
} catch { } catch {
console.error('Unable to decrypt message!') console.debug('Unable to decrypt message! Not for us...')
} }
}) })
this.sub = sub this.sub = sub
@ -146,33 +146,11 @@ async function chatDialog(path) {
} }
event.id = NostrTools.getEventHash(event) event.id = NostrTools.getEventHash(event)
event.sig = this.signEvent(event) event.sig = this.signEvent(event)
// This doesn't work yet
// this.pool.publish(Array.from(this.relays), event)
// this.newMessage = ''
// We need this hack
for (const url of Array.from(this.relays)) {
try {
let relay = NostrTools.relayInit(url)
relay.on('connect', () => {
console.debug(`connected to ${relay.url}`)
})
relay.on('error', () => {
console.debug(`failed to connect to ${relay.url}`)
})
await relay.connect() let pub = this.pool.publish(Array.from(this.relays), event)
let pub = relay.publish(event) pub.on('ok', () => console.debug(`Event was sent`))
pub.on('ok', () => { pub.on('failed', error => console.error(error))
console.debug(`${relay.url} has accepted our event`) this.newMessage = ''
})
pub.on('failed', reason => {
console.debug(`failed to publish to ${relay.url}: ${reason}`)
})
this.newMessage = ''
} catch (e) {
console.error(e)
}
}
}, },
async encryptMsg() { async encryptMsg() {
try { try {

View file

@ -274,32 +274,35 @@ async function customerStall(path) {
await this.sendOrder(event) await this.sendOrder(event)
}, },
async sendOrder(order) { async sendOrder(order) {
for (const url of Array.from(this.relays)) { let pub = this.pool.publish(Array.from(this.relays), order)
try { pub.on('ok', () => console.debug(`Order event was sent`))
let relay = NostrTools.relayInit(url) pub.on('failed', error => console.error(error))
relay.on('connect', () => { // for (const url of Array.from(this.relays)) {
console.debug(`connected to ${relay.url}`) // try {
}) // let relay = NostrTools.relayInit(url)
relay.on('error', () => { // relay.on('connect', () => {
console.debug(`failed to connect to ${relay.url}`) // console.debug(`connected to ${relay.url}`)
relay.close() // })
return // relay.on('error', () => {
}) // console.debug(`failed to connect to ${relay.url}`)
// relay.close()
// return
// })
await relay.connect() // await relay.connect()
let pub = relay.publish(order) // let pub = relay.publish(order)
pub.on('ok', () => { // pub.on('ok', () => {
console.debug(`${relay.url} has accepted our event: ${order.id}`) // console.debug(`${relay.url} has accepted our event: ${order.id}`)
relay.close() // relay.close()
}) // })
pub.on('failed', reason => { // pub.on('failed', reason => {
console.debug(`failed to publish to ${relay.url}: ${reason}`) // console.debug(`failed to publish to ${relay.url}: ${reason}`)
relay.close() // relay.close()
}) // })
} catch (err) { // } catch (err) {
console.error(`Error: ${err}`) // console.error(`Error: ${err}`)
} // }
} // }
this.loading = false this.loading = false
this.resetCart() this.resetCart()
this.openQrCodeDialog() this.openQrCodeDialog()
@ -308,7 +311,7 @@ async function customerStall(path) {
async listenMessages() { async listenMessages() {
this.loading = true this.loading = true
try { try {
const pool = new NostrTools.SimplePool() // const pool = new NostrTools.SimplePool()
const filters = [ const filters = [
{ {
kinds: [4], kinds: [4],
@ -320,7 +323,7 @@ async function customerStall(path) {
} }
] ]
let relays = Array.from(this.relays) let relays = Array.from(this.relays)
let subs = pool.sub(relays, filters) let subs = this.pool.sub(relays, filters)
subs.on('event', async event => { subs.on('event', async event => {
let mine = event.pubkey == this.customerPubkey let mine = event.pubkey == this.customerPubkey
let sender = mine let sender = mine
@ -341,7 +344,7 @@ async function customerStall(path) {
) )
} }
this.messageFilter(plaintext, cb => Promise.resolve(pool.close)) this.messageFilter(plaintext, cb => subs.unsub())
} catch { } catch {
console.debug('Unable to decrypt message! Probably not for us!') console.debug('Unable to decrypt message! Probably not for us!')
} }
@ -355,9 +358,8 @@ async function customerStall(path) {
let json = JSON.parse(text) let json = JSON.parse(text)
if (json.id != this.activeOrder) return if (json.id != this.activeOrder) return
if (json.payment_options) { if (json.payment_options) {
let payment_request = json.payment_options.find( let payment_request = json.payment_options.find(o => o.type == 'ln')
o => o.type == 'ln' .link
).link
if (!payment_request) return if (!payment_request) return
this.loading = false this.loading = false
this.qrCodeDialog.data.payment_request = payment_request this.qrCodeDialog.data.payment_request = payment_request
@ -373,7 +375,7 @@ async function customerStall(path) {
icon: 'thumb_up' icon: 'thumb_up'
}) })
this.activeOrder = null this.activeOrder = null
Promise.resolve(cb()) return cb()
} else { } else {
return return
} }