-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebclienthub.go
99 lines (81 loc) · 2.44 KB
/
webclienthub.go
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
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func (r *http.Request) bool { return true },
}
type WebClientHub struct {
webClients map[*WebClient]bool
register chan *WebClient
unregister chan *WebClient
newPhotoFiles chan *PhotoFile
photoFileService *PhotoFileService
}
func NewWebClientHub(photoFileService *PhotoFileService) *WebClientHub {
hub := new(WebClientHub)
hub.webClients = make(map[*WebClient]bool)
hub.register = make(chan *WebClient)
hub.unregister = make(chan *WebClient)
hub.photoFileService = photoFileService
newPhotoFiles := make(chan *PhotoFile)
photoFileService.RegisterNewPhotoListenerChan <- newPhotoFiles
hub.newPhotoFiles = newPhotoFiles
return hub
}
func (hub *WebClientHub) loop() {
for {
select {
case webClient := <-hub.register:
hub.registerWebClient(webClient)
case webClient := <-hub.unregister:
hub.unregisterWebClient(webClient)
case photoFile := <-hub.newPhotoFiles:
photoFile.String()
hub.broadcastPhotoFile(photoFile)
}
}
}
func (hub *WebClientHub) broadcastPhotoFile(photoFile *PhotoFile) {
log.Printf("Broadcast PhotoFile to %d WebClients | %s", len(hub.webClients), photoFile)
for webClient := range hub.webClients {
select {
case webClient.SendChan <- []byte(photoFile.String()):
default:
hub.unregisterWebClient(webClient)
}
}
}
func (hub *WebClientHub) registerWebClient(webClient *WebClient) {
hub.webClients[webClient] = true
log.Println("Registered WebClient with address", webClient)
for _, photoFile := range hub.photoFileService.GetRecentPhotoFiles() {
webClient.SendChan <- []byte(photoFile.String())
}
}
func (hub *WebClientHub) unregisterWebClient(webClient *WebClient) {
if _, ok := hub.webClients[webClient]; !ok {
return // given WebClient is not registered
}
delete(hub.webClients, webClient)
close(webClient.SendChan)
log.Println("Unregistered WebClient with address", webClient)
}
func (hub *WebClientHub) handleWebClientConnection(responseWriter http.ResponseWriter, request *http.Request) {
if request.Method != "GET" {
http.Error(responseWriter, "Method not allowed", 405)
return
}
webSocket, err := upgrader.Upgrade(responseWriter, request, nil)
if err != nil {
log.Println(err)
return
}
webClient := NewWebClient(webSocket, hub.unregister)
hub.register <- webClient
webClient.StartServing()
}