Exchange ids vs posts. Support image paste.

This commit is contained in:
bobbydigitales
2024-09-22 21:12:51 -07:00
parent 998840ec2c
commit c449267f01
11 changed files with 711 additions and 202 deletions

82
main.go
View File

@@ -69,21 +69,21 @@ type Peer struct {
lastActive time.Time
}
func removePeer(peerID string, peer *Peer) {
delete(peerConnections, peerID)
// func removePeer(peerID string, peer *Peer) {
// delete(peerConnections, peerID)
for userID, peers := range userPeers {
delete(peers, peerID)
if len(peers) == 0 {
delete(userPeers, userID)
}
}
// for userID, peers := range userPeers {
// delete(peers, peerID)
// if len(peers) == 0 {
// delete(userPeers, userID)
// }
// }
delete(connectionPeers, peer.conn)
// delete(connectionPeers, peer.conn)
// Close the peer's send channel
close(peer.send)
}
// // Close the peer's send channel
// close(peer.send)
// }
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
log.Println("Websocket connection!", r.RemoteAddr)
@@ -348,39 +348,39 @@ func main() {
Handler: nil, // Use the default ServeMux
}
// Start the inactivity monitor goroutine
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
// // Start the inactivity monitor goroutine
// go func() {
// ticker := time.NewTicker(10 * time.Second)
// defer ticker.Stop()
for {
select {
case <-done:
return
case <-ticker.C:
now := time.Now()
// for {
// select {
// case <-done:
// return
// case <-ticker.C:
// now := time.Now()
// Collect inactive peers
var inactivePeers []string
for peerID, peer := range peerConnections {
if now.Sub(peer.lastActive) > 60*time.Second {
inactivePeers = append(inactivePeers, peerID)
}
}
// // Collect inactive peers
// var inactivePeers []string
// for peerID, peer := range peerConnections {
// if now.Sub(peer.lastActive) > 60*time.Second {
// inactivePeers = append(inactivePeers, peerID)
// }
// }
// Remove inactive peers
for _, peerID := range inactivePeers {
peer := peerConnections[peerID]
// // Remove inactive peers
// for _, peerID := range inactivePeers {
// peer := peerConnections[peerID]
if peer != nil {
log.Printf("Peer %s inactive for more than 60 seconds. Closing connection.", peerID)
peer.conn.Close()
removePeer(peerID, peer)
}
}
}
}
}()
// if peer != nil {
// log.Printf("Peer %s inactive for more than 60 seconds. Closing connection.", peerID)
// peer.conn.Close()
// removePeer(peerID, peer)
// }
// }
// }
// }
// }()
// Run a goroutine to handle graceful shutdown
go func() {