-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathep_stu_api_events.go
More file actions
46 lines (36 loc) · 1.11 KB
/
ep_stu_api_events.go
File metadata and controls
46 lines (36 loc) · 1.11 KB
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
package main
import (
"context"
"log/slog"
"net/http"
"github.com/coder/websocket"
)
var upgraderOpts = &websocket.AcceptOptions{}
func (app *App) handleStuAPIEvents(w http.ResponseWriter, r *http.Request, sui *UserInfoStudent) {
app.logRequestStart(r, "handleStuAPIEvents", slog.Int64("student_id", sui.ID))
if r.Method != http.MethodGet {
app.apiError(r, w, http.StatusMethodNotAllowed, nil)
return
}
conn, err := websocket.Accept(w, r, upgraderOpts)
if err != nil {
app.logError(r, logMsgStudentEventsUpgradeError, slog.Any("error", err))
return
}
client := &Client{
conn: conn,
send: make(chan WSMessage, 256),
hub: app.wsHub,
studentID: sui.ID,
}
app.wsHub.register <- client
if err := conn.Write(context.Background(), websocket.MessageText, []byte("hello")); err != nil {
app.logError(r, logMsgStudentEventsHelloError, slog.Any("error", err))
app.wsHub.unregister <- client
_ = conn.Close(websocket.StatusInternalError, "")
return
}
go client.writePump()
go client.readPump()
app.logInfo(r, logMsgStudentEventsEstablished, slog.Int64("student_id", sui.ID))
}