-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsockerHandler.go
More file actions
73 lines (63 loc) · 2.4 KB
/
websockerHandler.go
File metadata and controls
73 lines (63 loc) · 2.4 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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func addUserToIncident(incidentID string, userSocket *websocket.Conn) {
incidentSocketCache[incidentID] = append(incidentSocketCache[incidentID], userSocket)
updateIncidentUserCount(incidentID)
}
func pushMessageToSubscribers(incidentID string, message string) {
for _, socket := range incidentSocketCache[incidentID] {
socket.WriteMessage(websocket.TextMessage, []byte(message))
}
}
func updateIncidentUserCount(incidentID string) {
numResponders := strconv.Itoa(len(incidentSocketCache[incidentID]) - 1) // -1 Because Requester is in here
pushMessageToSubscribers(incidentID, numResponders)
}
func closeIncidentSockets(incidentID string) {
for _, socket := range incidentSocketCache[incidentID] {
socket.Close()
}
delete(incidentSocketCache, incidentID)
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
failWithStatusCode(err, http.StatusText(http.StatusBadRequest), w, http.StatusBadRequest)
return
}
message := SocketMessage{}
// frontend handshake to get user and hook them into the userMap for sockets
err = conn.ReadJSON(&message)
if err != nil {
failWithStatusCode(err, "Failed to handshake", w, http.StatusInternalServerError)
return
}
if len(message.UserID) == 15 {
// new Incident from IMEI
incident, found := incidentSocketCache[message.IncidentID]
if found {
// Another request is being opened from the same IMEI. Das bad
fmt.Print(incident)
//Probs close all sockets and start over
// Close all sockets
//userSocket.Close()
}
//IncidentEventObj := &IncidentEvent{Requester: conn}
//incidentSocketCache[message.IncidentID] = &IncidentEvent{}
incidentSocketCache[message.IncidentID] = append(incidentSocketCache[message.IncidentID], conn)
fmt.Printf("%+v", incidentSocketCache[message.IncidentID])
}
userSocketCache[message.UserID] = conn
//conn.WriteMessage(websocket.TextMessage, []byte("4"))
fmt.Printf("Handshake from client is %+v\n", message)
fmt.Printf("Incident Table looks like %+v\n", incidentSocketCache)
}