aboutsummaryrefslogtreecommitdiff
path: root/websocket_server.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-09-29 00:37:57 +0700
committerShulhan <ms@kilabit.info>2021-09-29 00:37:57 +0700
commitf9c01618f3a59c257ee601d3cf4de5b0e3d73675 (patch)
treec8fdb27f6325d2e4092778d4e69b1cd6df86391b /websocket_server.go
parentafb02f3df1f440c16e5a3c4fc1b117eac6d029dc (diff)
downloadgorankusu-f9c01618f3a59c257ee601d3cf4de5b0e3d73675.tar.xz
all: implement WebSocket notification when attack finished
When the attack finished, the WebSocket server will broadcast the result to all clients as message "/_trunks/api/attack/result".
Diffstat (limited to 'websocket_server.go')
-rw-r--r--websocket_server.go33
1 files changed, 30 insertions, 3 deletions
diff --git a/websocket_server.go b/websocket_server.go
index 80d49d2..4079ed1 100644
--- a/websocket_server.go
+++ b/websocket_server.go
@@ -10,15 +10,42 @@ import (
"encoding/json"
"errors"
"fmt"
+ "log"
"net/http"
liberrors "github.com/shuLhan/share/lib/errors"
"github.com/shuLhan/share/lib/websocket"
)
-const (
- apiAttackHttp = "/_trunks/api/attack/http"
-)
+func (trunks *Trunks) wsBroadcastAttackFinish(result *AttackResult) {
+ if result == nil {
+ return
+ }
+
+ logp := "wsBroadcastAttackFinish"
+
+ jsonb, err := json.Marshal(result)
+ if err != nil {
+ log.Printf("%s: %s", logp, err)
+ return
+ }
+
+ packet, err := websocket.NewBroadcast(
+ apiAttackResult,
+ base64.StdEncoding.EncodeToString(jsonb),
+ )
+ if err != nil {
+ log.Printf("%s: %s", logp, err)
+ return
+ }
+
+ for _, conn := range trunks.Wsd.Clients.All() {
+ err = websocket.Send(conn, packet)
+ if err != nil {
+ log.Printf("%s: %s", logp, err)
+ }
+ }
+}
func (trunks *Trunks) initWebSocketServer() (err error) {
opts := &websocket.ServerOptions{