-
Notifications
You must be signed in to change notification settings - Fork 102
/
eps_wsocket.go
106 lines (95 loc) · 2.35 KB
/
eps_wsocket.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2018 PT Defender Nusa Semesta and contributors, All rights reserved.
//
// This file is part of Dsiem.
//
// Dsiem is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3 of the License.
//
// Dsiem is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Dsiem. If not, see <https://www.gnu.org/licenses/>.
package server
import (
"github.com/defenxor/dsiem/internal/pkg/shared/idgen"
log "github.com/defenxor/dsiem/internal/pkg/shared/logger"
"github.com/fasthttp-contrib/websocket"
// "golang.org/x/net/websocket"
)
type client struct {
id string
ws *websocket.Conn
}
type message struct {
Eps int64 `json:"eps"`
}
type wsServer struct {
clients map[string]*client
sendAllCh chan *message
cConnectedCh chan bool
}
func newWSServer() *wsServer {
clients := make(map[string]*client)
sendAllCh := make(chan *message)
cConnectedCh := make(chan bool)
return &wsServer{
clients,
sendAllCh,
cConnectedCh,
}
}
func (s *wsServer) add(ws *websocket.Conn) (id string, err error) {
id, err = idgen.GenerateID()
if err != nil {
log.Debug(log.M{Msg: "cannot create an ID for WS client!" + id})
return "", err
}
log.Debug(log.M{Msg: "adding WS client " + id})
c := client{}
c.id = id
c.ws = ws
s.clients[c.id] = &c
// non-blocking signal
select {
case s.cConnectedCh <- true:
default:
}
return c.id, nil
}
func (s *wsServer) del(cID string) {
delete(s.clients, cID)
}
func (s *wsServer) sendAll(msg *message) {
s.sendAllCh <- msg
}
func (s *wsServer) onClientConnected(ws *websocket.Conn) {
defer func() {
_ = ws.Close()
}()
id, err := s.add(ws)
if err != nil {
return
}
for {
select {
// send message to client
case msg := <-s.sendAllCh:
/*
_, err := json.Marshal(msg)
if err != nil {
log.Debug(log.M{Msg: "failed to marshal msg"})
continue
}
*/
if err := ws.WriteJSON(msg); err != nil {
log.Debug(log.M{Msg: "failed to write to " + id + ", assuming client is disconnected."})
s.del(id)
return
}
}
}
}