-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
CalebRose
committed
Oct 8, 2024
1 parent
e4be298
commit 5b00a82
Showing
7 changed files
with
426 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/CalebRose/SimFBA/managers" | ||
"github.com/CalebRose/SimFBA/structs" | ||
) | ||
|
||
func GetUpdatedTimestamp() structs.Timestamp { | ||
ts := managers.GetTimestamp() | ||
return ts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ws | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/CalebRose/SimFBA/structs" | ||
) | ||
|
||
// BroadcastTSUpdate sends the updated timestamp to all connected WebSocket clients | ||
func BroadcastTSUpdate(ts structs.Timestamp) { | ||
for conn := range clients { | ||
err := conn.WriteJSON(ts) | ||
if err != nil { | ||
log.Println("Error broadcasting to WebSocket client:", err) | ||
conn.Close() | ||
delete(clients, conn) // Remove client on error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ws | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/CalebRose/SimFBA/managers" | ||
"github.com/gorilla/websocket" | ||
) | ||
|
||
// Upgrader configures the WebSocket connection | ||
var upgrader = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: func(r *http.Request) bool { | ||
// In production, you should validate origins for security | ||
return true | ||
}, | ||
} | ||
|
||
// Global map to keep track of connected WebSocket clients | ||
var clients = make(map[*websocket.Conn]bool) | ||
|
||
// WebSocketHandler handles WebSocket connection requests | ||
func WebSocketHandler(w http.ResponseWriter, r *http.Request) { | ||
// Upgrade the HTTP request to a WebSocket connection | ||
conn, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Println("WebSocket upgrade failed:", err) | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
// Add new WebSocket connection to the clients map | ||
clients[conn] = true | ||
log.Println("New WebSocket client connected") | ||
|
||
ts := managers.GetTimestamp() | ||
// Send the latest timestamp to the user immediately upon connection | ||
err = conn.WriteJSON(ts) | ||
if err != nil { | ||
log.Println("Error sending timestamp:", err) | ||
return | ||
} | ||
|
||
// Listen for messages (e.g., handle disconnects) | ||
for { | ||
_, msg, err := conn.ReadMessage() | ||
if err != nil { | ||
log.Println("WebSocket client disconnected:", err) | ||
conn.Close() | ||
delete(clients, conn) | ||
break | ||
} | ||
|
||
if err := conn.WriteMessage(websocket.TextMessage, msg); err != nil { | ||
log.Println("Error writing WebSocket message:", err) | ||
break | ||
} | ||
} | ||
} |