forked from hidevopsio/hiboot
/
statushandler.go
38 lines (30 loc) · 872 Bytes
/
statushandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package service
import (
"fmt"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/at"
"hidevops.io/hiboot/pkg/log"
"hidevops.io/hiboot/pkg/starter/websocket"
)
// StatusHandler is the websocket handler
type StatusHandler struct {
at.ContextAware
connection *websocket.Connection
}
func newStatusHandler(connection *websocket.Connection) *StatusHandler {
h := &StatusHandler{connection: connection}
return h
}
func init() {
app.Register(newStatusHandler)
}
// OnMessage is the websocket message handler
func (h *StatusHandler) OnMessage(data []byte) {
message := string(data)
log.Debugf("client: %v", message)
h.connection.EmitMessage([]byte(fmt.Sprintf("Status: Up")))
}
// OnDisconnect is the websocket disconnection handler
func (h *StatusHandler) OnDisconnect() {
log.Debugf("Connection with ID: %v has been disconnected!", h.connection.ID())
}