|
| 1 | +package session |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/gorilla/websocket" |
| 9 | +) |
| 10 | + |
| 11 | +/* handle websocket commands from clients */ |
| 12 | +func (m *Manager)handleWebSocketCommands(conn *websocket.Conn, ctxVal context.Context, cancel context.CancelFunc) { |
| 13 | + defer cancel() |
| 14 | + |
| 15 | + /* infinite loop */ |
| 16 | + for { |
| 17 | + var msg map[string]interface{} |
| 18 | + err := conn.ReadJSON(&msg) |
| 19 | + if err != nil { |
| 20 | + if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { |
| 21 | + m.errCh<-fmt.Errorf("websocket error: %w", err) |
| 22 | + } |
| 23 | + break |
| 24 | + } |
| 25 | + |
| 26 | + /* handle commands from clients */ |
| 27 | + if msgType, ok := msg["type"].(string); ok { |
| 28 | + switch msgType { |
| 29 | + |
| 30 | + /* ping echo test */ |
| 31 | + case "ping": |
| 32 | + pongMsg := StreamMessage{ |
| 33 | + Type: "pong", |
| 34 | + Data: "pong", |
| 35 | + Timestamp: time.Now(), |
| 36 | + } |
| 37 | + if err := conn.WriteJSON(pongMsg); err != nil { |
| 38 | + m.errCh<-fmt.Errorf("failed to send pong: %w", err) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + /* refresh content served */ |
| 43 | + case "refresh": |
| 44 | + /* client requests fresh data - implement based on current context */ |
| 45 | + val := ctxVal.Value("type") |
| 46 | + |
| 47 | + switch val { |
| 48 | + case StreamUserSession: |
| 49 | + /* push user session */ |
| 50 | + case StreamUserTransactions: |
| 51 | + /* push user transactions */ |
| 52 | + case StreamAllSessions: |
| 53 | + /* push all sessions */ |
| 54 | + case StreamAllTransactions: |
| 55 | + /* push all transactions */ |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments