Skip to content

Commit

Permalink
checkUrl https
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivanov N committed Dec 15, 2024
1 parent 10da015 commit 47b6f0a
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions src/store/plugins/navigatorOnline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { i18n } from '@/i18n'
import axios from 'axios'

const checkUrl = 'http://ipv4.icanhazip.com'
const checkUrl = 'https://ipv4.icanhazip.com'

export default (store) => {
// window.addEventListener('online', handleEvent)
Expand All @@ -14,33 +14,31 @@ export default (store) => {
// store.commit('setIsOnline', false)
// }

setInterval(async () => {
const res = Boolean(await inetConnectionCheck())
const storeVal = store.getters['isOnline']
if (storeVal !== res) {
store.commit('setIsOnline', res)
const snackMsg = res ? 'online' : 'offline'
store.dispatch('snackbar/show', {
message: i18n.global.t(`connection.${snackMsg}`),
timeout: 3000
})
}
}, 1000)
}

const inetConnectionCheck = async () => {
try {
const res = await axios.get(`${checkUrl}`,
{
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
},
}
)
return res.status >=200 && res.status < 300
} catch(err) {
if (err.code === 'ERR_NETWORK') return false
setInterval(async () => {
const res = Boolean(await inetConnectionCheck())
const storeVal = store.getters['isOnline']
if (storeVal !== res) {
store.commit('setIsOnline', res)
const snackMsg = res ? 'online' : 'offline'
store.dispatch('snackbar/show', {
message: i18n.global.t(`connection.${snackMsg}`),
timeout: 3000
})
}
}, 1000)
}

const inetConnectionCheck = async () => {
try {
const res = await axios.get(`${checkUrl}`, {
headers: {
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: '0'
}
})
return res.status >= 200 && res.status < 300
} catch (err) {
if (err.code === 'ERR_NETWORK') return false
}
}

0 comments on commit 47b6f0a

Please sign in to comment.