-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketServer.go
283 lines (242 loc) · 5.62 KB
/
WebSocketServer.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
_ "embed"
"encoding/json"
"io/fs"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"golang.org/x/exp/slog"
"github.com/gorilla/handlers"
"github.com/gorilla/websocket"
)
//go:embed Resource/web/index.html
var QueHtmlFile []byte
//go:embed Resource/web/default.css
var cssFile []byte
//go:embed Resource/web/DmDisplay.html
var DmDisplayHtml []byte
//go:embed Resource/web/js/NoSleep.min.js
var NoSleepJs []byte
// 使用互斥锁保护共享资源
var (
queueLock sync.Mutex
dmLock sync.Mutex
)
var (
QueueChatChan = make(chan []byte, 50)
DmChatChan = make(chan []byte, 50)
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
QueueConnMap = make(map[*websocket.Conn]bool)
DmConnMap = make(map[*websocket.Conn]bool)
)
func StartWebServer() {
_, _ = http.Get("http://127.0.0.1:100/EXIT")
handler := handlers.CORS(
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowCredentials(),
)(WebServer())
err := http.ListenAndServe(":100", handler)
if err != nil {
slog.Error(err.Error())
return
}
}
func WebServer() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/LineWs", func(writer http.ResponseWriter, request *http.Request) {
conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
slog.Error(err.Error())
return
}
QueueConnMap[conn] = true
err = conn.WriteMessage(websocket.TextMessage, []byte("Connected"))
if err != nil {
delete(QueueConnMap, conn)
return
}
defer func(conn *websocket.Conn) {
err := conn.Close()
if err != nil {
slog.Error("Failed to close connection:", err)
return
}
}(conn)
go func() {
for {
_, Message, err := conn.ReadMessage()
if err != nil {
return
}
switch string(Message) {
case "ping":
err := conn.WriteMessage(websocket.TextMessage, []byte("pong"))
if err != nil {
return
}
}
}
}()
for {
Chat := <-QueueChatChan
ConnMapCopy := QueueConnMap
for w := range ConnMapCopy {
err = w.WriteMessage(websocket.TextMessage, Chat)
if err != nil {
slog.Error("Failed to write message:", err)
delete(QueueConnMap, w)
}
}
}
})
mux.HandleFunc("/DmWs", func(writer http.ResponseWriter, request *http.Request) {
conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
slog.Error("Websocket Upgrade Err:", err.Error())
return
}
DmConnMap[conn] = true
err = conn.WriteMessage(websocket.TextMessage, []byte("Connected"))
if err != nil {
slog.Error("Websocket Write Err:", err.Error())
delete(DmConnMap, conn)
return
}
defer func(conn *websocket.Conn) {
err := conn.Close()
if err != nil {
slog.Error("Failed to close connection:", err)
return
}
}(conn)
go func() {
for {
_, Message, err := conn.ReadMessage()
if err != nil {
return
}
switch string(Message) {
case "ping":
err := conn.WriteMessage(websocket.TextMessage, []byte("pong"))
if err != nil {
return
}
}
}
}()
for {
Chat := <-DmChatChan
ConnMapCopy := DmConnMap
for w := range ConnMapCopy {
err = w.WriteMessage(websocket.TextMessage, Chat)
if err != nil {
slog.Error("Failed to write message:", err)
delete(DmConnMap, w)
}
}
}
})
// 静态资源响应
mux.HandleFunc("/web", func(writer http.ResponseWriter, request *http.Request) {
_, err := writer.Write(QueHtmlFile)
if err != nil {
return
}
})
mux.HandleFunc("/dm", func(writer http.ResponseWriter, request *http.Request) {
_, err := writer.Write(DmDisplayHtml)
if err != nil {
return
}
})
mux.HandleFunc("/font.ttf", func(writer http.ResponseWriter, request *http.Request) {
err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return nil
}
if strings.HasSuffix(info.Name(), ".ttf") {
file, err := os.ReadFile(path)
if err != nil {
return err
}
_, err = writer.Write(file)
if err != nil {
return err
}
}
return nil
})
if err != nil {
slog.Error("Find font err", err)
return
}
})
mux.HandleFunc("/default.css", func(writer http.ResponseWriter, request *http.Request) {
var found bool
dir, err := os.ReadDir("./")
if err != nil {
return
}
for _, file := range dir {
if strings.HasSuffix(file.Name(), ".css") {
found = true
readFile, err := os.ReadFile(file.Name())
if err != nil {
return
}
_, err = writer.Write(readFile)
}
}
if !found {
_, err := writer.Write(cssFile)
if err != nil {
return
}
}
})
mux.HandleFunc("/NoSleep.min.js", func(writer http.ResponseWriter, request *http.Request) {
_, err := writer.Write(NoSleepJs)
if err != nil {
return
}
})
// 静态同步接口
mux.HandleFunc("/getAllLine", func(writer http.ResponseWriter, request *http.Request) {
lineJson, err := json.Marshal(line)
if err != nil {
return
}
_, err = writer.Write(lineJson)
if err != nil {
return
}
})
mux.HandleFunc("/getLineLength", func(writer http.ResponseWriter, request *http.Request) {
LineLength := len(line.GuardLine) + len(line.GiftLine) + len(line.CommonLine)
_, err := writer.Write([]byte(strconv.Itoa(LineLength)))
if err != nil {
return
}
})
mux.HandleFunc("/getConfig", func(writer http.ResponseWriter, request *http.Request) {
ConfigJsonByte, err := json.Marshal(globalConfiguration)
if err != nil {
return
}
_, err = writer.Write(ConfigJsonByte)
if err != nil {
return
}
})
mux.HandleFunc("/EXIT", func(writer http.ResponseWriter, request *http.Request) {
os.Exit(0)
})
return mux
}