This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip.go
101 lines (83 loc) · 2.56 KB
/
gossip.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
100
101
package main
import (
"net/http"
"log"
"os"
"encoding/json"
)
type Message struct {
Name string `json:"name"`
Body string `json:"body"`
}
type Client struct {
Channel chan <- Message
}
func handleMessages(messageChan <- chan Message, addChan <- chan Client, removeChan <- chan Client) {
channels := make(map[Client] chan <- Message)
for {
select {
case message := <- messageChan:
log.Print("New message: ", message.Body)
for _, channel := range channels {
go func (c chan <- Message) {
c <- message
}(channel)
}
case client := <- addChan:
log.Print("Client connected: ", client)
channels[client] = client.Channel
case client := <- removeChan:
log.Print("Client disconnected: ", client)
delete(channels, client)
}
}
}
func handleStream(messageChan chan <- Message, addChan chan <- Client, removeChan chan <- Client, writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "text/event-stream")
writer.Header().Set("Access-Control-Allow-Origin", "*")
writer.WriteHeader(200)
channel := make(chan Message)
client := Client{channel}
addChan <- client
for {
message := <- channel
data, _ := json.Marshal(message)
if _, error := writer.Write([]byte("data: " + string(data) + "\n\n")); error != nil {
log.Print("Write: ", error)
break
}
writer.(http.Flusher).Flush()
}
removeChan <- client
}
func handleMessage(messageChan chan <- Message, writer http.ResponseWriter, request *http.Request) {
request.ParseForm()
message := request.FormValue("message")
name := request.FormValue("name")
messageChan <- Message{name, message}
writer.WriteHeader(200)
}
func main() {
messagesChan := make(chan Message)
addChan := make(chan Client)
removeChan := make(chan Client)
go handleMessages(messagesChan, addChan, removeChan)
http.HandleFunc("/", func (writer http.ResponseWriter, request *http.Request) {
http.ServeFile(writer, request, "static/index.html")
})
http.HandleFunc("/static/", func (writer http.ResponseWriter, request *http.Request) {
http.ServeFile(writer, request, request.URL.Path[1:])
})
http.HandleFunc("/stream", func (writer http.ResponseWriter, request *http.Request) {
handleStream(messagesChan, addChan, removeChan, writer, request)
})
http.HandleFunc("/messages", func (writer http.ResponseWriter, request *http.Request) {
handleMessage(messagesChan, writer, request)
})
port := "8080"
if portFromEnv := os.Getenv("PORT"); portFromEnv != "" {
port = portFromEnv
}
log.Print("Starting server on port ", port)
http.ListenAndServe(":" + port, nil)
}