-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
143 lines (123 loc) · 3.46 KB
/
server.go
File metadata and controls
143 lines (123 loc) · 3.46 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
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
package main
import (
"embed"
"encoding/gob"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
"tmuxviewer/tmux"
"tmuxviewer/shared"
)
//go:embed all:static
var staticFiles embed.FS
type Server struct {
sessionManager *tmux.SessionManager
hub *Hub
}
func NewServer() *Server {
return &Server{
sessionManager: tmux.NewSessionManager(),
hub: NewHub(),
}
}
func (s *Server) Start(addr string) error {
s.sessionManager.Start()
go s.hub.Run()
go s.processUpdates()
http.HandleFunc("/ws", s.handleWebSocket)
http.Handle("/", http.FileServer(http.Dir("static")))
log.Printf("Starting server on %s", addr)
return http.ListenAndServe(addr, nil)
}
func (s *Server) processUpdates() {
for update := range s.sessionManager.Updates() {
switch update.Type {
case tmux.UpdateTypeSessionList:
sessions := s.sessionManager.GetSessions()
log.Printf("Processing session list update, found %d sessions", len(sessions))
msg := &shared.Message{
Type: shared.MessageTypeSessionList,
Data: sessions,
Timestamp: time.Now(),
}
s.hub.Broadcast(msg)
case tmux.UpdateTypePaneContent:
log.Printf("Broadcasting pane update for pane %s with %d line updates",
update.Pane.PaneID, len(update.Pane.Lines))
msg := &shared.Message{
Type: shared.MessageTypePaneUpdate,
PaneID: update.Pane.PaneID,
Data: update.Pane,
Timestamp: time.Now(),
}
s.hub.Broadcast(msg)
case tmux.UpdateTypeLoadingStatus:
// Temporarily skip loading status messages to debug
log.Printf("Skipping loading status for pane %s: %d%%",
update.LoadingStatus.PaneID, update.LoadingStatus.Progress)
continue
case tmux.UpdateTypeTmuxStatus:
log.Printf("TMux status changed: %v", update.TmuxStatus.IsRunning)
msg := &shared.Message{
Type: shared.MessageTypeTmuxStatus,
Data: update.TmuxStatus,
Timestamp: time.Now(),
}
s.hub.Broadcast(msg)
}
}
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func (s *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("WebSocket upgrade error: %v", err)
return
}
client := &Client{
hub: s.hub,
conn: conn,
send: make(chan *shared.Message, 256),
encoder: gob.NewEncoder(nil),
decoder: gob.NewDecoder(nil),
}
s.hub.Register(client)
log.Printf("Starting WritePump and ReadPump for new client")
go client.WritePump()
go client.ReadPump()
// Give the pumps time to start
log.Printf("Waiting for pumps to initialize...")
time.Sleep(100 * time.Millisecond)
log.Printf("Done waiting, getting sessions...")
sessions := s.sessionManager.GetSessions()
log.Printf("Sending initial session list to new client, %d sessions", len(sessions))
msg := &shared.Message{
Type: shared.MessageTypeSessionList,
Data: sessions,
Timestamp: time.Now(),
}
client.send <- msg
// Send initial pane content for all panes
log.Println("Sending initial pane content to new client")
for _, session := range sessions {
for _, window := range session.Windows {
for _, pane := range window.Panes {
content := s.sessionManager.GetPaneContent(pane.ID)
if content != nil {
paneMsg := &shared.Message{
Type: shared.MessageTypePaneUpdate,
PaneID: pane.ID,
Data: content,
Timestamp: time.Now(),
}
client.send <- paneMsg
}
}
}
}
}