/
websocket.go
69 lines (53 loc) · 2.16 KB
/
websocket.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package handler
import (
"encoding/json"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
"github.com/aaronchen2k/deeptest/internal/pkg/helper/websocket"
"github.com/aaronchen2k/deeptest/internal/server/modules/service"
"github.com/aaronchen2k/deeptest/pkg/domain"
_i118Utils "github.com/aaronchen2k/deeptest/pkg/lib/i118"
_logUtils "github.com/aaronchen2k/deeptest/pkg/lib/log"
"github.com/kataras/iris/v12/websocket"
)
const (
result = "result"
outPut = "output"
)
var (
ch chan int
)
type WebSocketCtrl struct {
Namespace string
*websocket.NSConn `stateless:"true"`
ScenarioExecService *service.ScenarioExecService `inject:""`
}
func NewWebsocketCtrl() *WebSocketCtrl {
inst := &WebSocketCtrl{Namespace: consts.WsDefaultNamespace}
return inst
}
func (c *WebSocketCtrl) OnNamespaceConnected(wsMsg websocket.Message) error {
websocketHelper.SetConn(c.Conn)
_logUtils.Infof(_i118Utils.Sprintf("ws_namespace_connected", c.Conn.ID(), wsMsg.Room))
resp := _domain.WsResp{Msg: "from server: connected to websocket"}
bytes, _ := json.Marshal(resp)
mqData := _domain.MqMsg{Namespace: wsMsg.Namespace, Room: wsMsg.Room, Event: wsMsg.Event, Content: string(bytes)}
websocketHelper.PubMsg(mqData)
return nil
}
// OnNamespaceDisconnect
// This will call the "OnVisit" event on all clients, except the current one,
// it can't because it's left but for any case use this type of design.
func (c *WebSocketCtrl) OnNamespaceDisconnect(wsMsg websocket.Message) error {
_logUtils.Infof(_i118Utils.Sprintf("ws_namespace_disconnected", c.Conn.ID()))
resp := _domain.WsResp{Msg: "from server: disconnected to websocket"}
bytes, _ := json.Marshal(resp)
mqData := _domain.MqMsg{Namespace: wsMsg.Namespace, Room: wsMsg.Room, Event: wsMsg.Event, Content: string(bytes)}
websocketHelper.PubMsg(mqData)
return nil
}
// OnChat This will call the "OnVisit" event on all clients, including the current one, with the 'newCount' variable.
func (c *WebSocketCtrl) OnChat(wsMsg websocket.Message) (err error) {
ctx := websocket.GetContext(c.Conn)
_logUtils.Infof("WebSocket OnChat: remote address=%s, room=%s, msg=%s", ctx.RemoteAddr(), wsMsg.Room, string(wsMsg.Body))
return
}