Skip to content

Commit

Permalink
Fix Notify WS
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Dec 20, 2023
1 parent 5c8d160 commit 2280b08
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/components/elements/NotifyPolling.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NotifyPolling extends React.Component {

break
} catch (err) {
console.warn('counterSubscribeWs:', err, ', retry...')
console.warn('counterSubscribeWs:', err, username, ', retry...')
await delay(500)
}
}
Expand Down
49 changes: 33 additions & 16 deletions app/utils/NotifyApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,30 @@ function saveSession(response) {
}

async function connectNotifyWs() {
if (!window.notifyWs) {
if (!window.notifyWs || window.notifyWs.readyState !== 1) {
window.notifyWsReq = { id: 0, requests: {}, callbacks: {} }
if (window.notifyWs) {
window.notifyWs.close()
}
await new Promise((resolve, reject) => {
const notifyWs = new WebSocket($STM_Config.notify_service.host_ws)
window.notifyWs = notifyWs

const timeout = setTimeout(() => {
if (notifyWs && !notifyWs.isOpen) {
reject(new Error('Cannot connect Notify WS'))
}
}, 5000)

notifyWs.addEventListener('open', () => {
notifyWs.isOpen = true
clearTimeout(timeout)
resolve()
})

notifyWs.addEventListener('сlose', () => {
if (!notifyWs.isOpen) {
clearTimeout(timeout)
const err = new Error('notifyWs - cannot connect')
reject(err)
}
Expand Down Expand Up @@ -93,22 +104,28 @@ async function connectNotifyWs() {
}

async function notifyWsSend(api, args, callback = null, eventCallback = null) {
await connectNotifyWs()
const id = window.notifyWsReq.id++
let msg = {
api,
args,
id
}
msg = JSON.stringify(msg)
if (callback) {
window.notifyWsReq.requests[id] = { callback }
}
if (eventCallback) {
const { event, callback } = eventCallback
window.notifyWsReq.callbacks[event] = { callback }
try {
await connectNotifyWs()
const id = window.notifyWsReq.id++
let msg = {
api,
args,
id
}
msg = JSON.stringify(msg)
if (callback) {
window.notifyWsReq.requests[id] = { callback }
}
if (eventCallback) {
const { event, callback } = eventCallback
window.notifyWsReq.callbacks[event] = { callback }
}
window.notifyWs.send(msg)
} catch (err) {
if (callback) {
callback(err, null)
}
}
window.notifyWs.send(msg)
}

export function notifyApiLogin(account, authSession) {
Expand Down

0 comments on commit 2280b08

Please sign in to comment.