forked from hidevopsio/hiboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counthandler.go
44 lines (38 loc) · 962 Bytes
/
counthandler.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
39
40
41
42
43
44
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"
"time"
)
// CountHandler is the websocket handler
type CountHandler struct {
at.ContextAware
connection *websocket.Connection
}
func newCountHandler(connection *websocket.Connection) *CountHandler {
h := &CountHandler{connection: connection}
return h
}
func init() {
app.Register(newCountHandler)
}
// OnMessage is the websocket message handler
func (h *CountHandler) OnMessage(data []byte) {
message := string(data)
log.Debugf("client: %v", message)
var i int
go func() {
for {
i++
h.connection.EmitMessage([]byte(fmt.Sprintf("=== %v %d ===", message, i)))
time.Sleep(time.Second)
}
}()
}
// OnDisconnect is the websocket disconnection handler
func (h *CountHandler) OnDisconnect() {
log.Debugf("Connection with ID: %v has been disconnected!", h.connection.ID())
}