mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 11:39:17 +00:00
adding websocket ping/pong messages
This commit is contained in:
parent
f682d4c9d3
commit
58269ed541
@ -1,6 +1,7 @@
|
|||||||
package databag
|
package databag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -44,7 +45,7 @@ func Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
// extract token target and access
|
// extract token target and access
|
||||||
target, access, ret := ParseToken(a.AppToken)
|
target, access, ret := ParseToken(a.AppToken)
|
||||||
if ret != nil {
|
if ret != nil {
|
||||||
ErrMsg(err)
|
ErrMsg(ret)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,6 +77,10 @@ func Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
AddStatusListener(app.Account.ID, c)
|
AddStatusListener(app.Account.ID, c)
|
||||||
defer RemoveStatusListener(app.Account.ID, c)
|
defer RemoveStatusListener(app.Account.ID, c)
|
||||||
|
|
||||||
|
// start ping pong ticker
|
||||||
|
ticker := time.NewTicker(60 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
// send revision until channel is closed
|
// send revision until channel is closed
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -84,6 +89,12 @@ func Status(w http.ResponseWriter, r *http.Request) {
|
|||||||
ErrMsg(err)
|
ErrMsg(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
case <-ticker.C:
|
||||||
|
if err := conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||||
|
ErrMsg(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn.SetReadDeadline(time.Now().Add(15 * time.Second))
|
||||||
case <-wsExit:
|
case <-wsExit:
|
||||||
LogMsg("exiting server")
|
LogMsg("exiting server")
|
||||||
wsExit<-true
|
wsExit<-true
|
||||||
|
@ -50,7 +50,6 @@ func ErrResponse(w http.ResponseWriter, code int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ErrMsg(err error) {
|
func ErrMsg(err error) {
|
||||||
PrintMsg("IN ERR MSG")
|
|
||||||
if !hideLog && err != nil {
|
if !hideLog && err != nil {
|
||||||
_, file, line, _ := runtime.Caller(1)
|
_, file, line, _ := runtime.Caller(1)
|
||||||
p, _ := os.Getwd()
|
p, _ := os.Getwd()
|
||||||
|
@ -55,7 +55,7 @@ function App() {
|
|||||||
const [username, setUsername] = useState('')
|
const [username, setUsername] = useState('')
|
||||||
const [password, setPassword] = useState('')
|
const [password, setPassword] = useState('')
|
||||||
const [confirmed, setConfirmed] = useState('')
|
const [confirmed, setConfirmed] = useState('')
|
||||||
const [mode, setMode] = useState('login')
|
const [mode, setMode] = useState('')
|
||||||
const [creatable, setCreatable] = useState(false)
|
const [creatable, setCreatable] = useState(false)
|
||||||
const [conflict, setConflict] = useState('')
|
const [conflict, setConflict] = useState('')
|
||||||
const [token, setToken] = useState('')
|
const [token, setToken] = useState('')
|
||||||
@ -63,11 +63,21 @@ function App() {
|
|||||||
const ws = useRef(null)
|
const ws = useRef(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
let access = localStorage.getItem("access")
|
||||||
|
console.log("ACCESS", access)
|
||||||
|
|
||||||
|
if (access == null) {
|
||||||
|
setMode('login')
|
||||||
getAvailable().then(a => {
|
getAvailable().then(a => {
|
||||||
setAvailable(a > 0)
|
setAvailable(a > 0)
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
setMode('logout')
|
||||||
|
connectStatus(access)
|
||||||
|
}
|
||||||
|
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@ -110,6 +120,7 @@ function App() {
|
|||||||
ws.current.error = () => {
|
ws.current.error = () => {
|
||||||
console.log('ws error')
|
console.log('ws error')
|
||||||
}
|
}
|
||||||
|
localStorage.setItem("access", access)
|
||||||
}
|
}
|
||||||
|
|
||||||
const Logout = () => {
|
const Logout = () => {
|
||||||
@ -171,6 +182,8 @@ function App() {
|
|||||||
const onLogout = () => {
|
const onLogout = () => {
|
||||||
ws.current.onclose = () => {}
|
ws.current.onclose = () => {}
|
||||||
ws.current.close(1000, "bye")
|
ws.current.close(1000, "bye")
|
||||||
|
ws.current = null
|
||||||
|
localStorage.removeItem("access")
|
||||||
setMode('login')
|
setMode('login')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user