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