Skip to content

Commit

Permalink
Enable the websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebRose committed Oct 8, 2024
1 parent e4be298 commit 5b00a82
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 271 deletions.
11 changes: 11 additions & 0 deletions controller/WebsocketController.go
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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/gorilla/websocket v1.5.3
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/nelkinda/http-go v0.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyE
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
598 changes: 328 additions & 270 deletions main.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion managers/AdminManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func GetTimestamp() structs.Timestamp {

var timestamp structs.Timestamp

db.First(&timestamp)
err := db.First(&timestamp).Error
if err != nil {
log.Printf("Error querying for timestamp: %v", err)
}

return timestamp
}
Expand Down
19 changes: 19 additions & 0 deletions ws/broadcast.go
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
}
}
}
61 changes: 61 additions & 0 deletions ws/websocket.go
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
}
}
}

0 comments on commit 5b00a82

Please sign in to comment.