-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.go
66 lines (59 loc) · 1.47 KB
/
ws.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
package main
import (
"flag"
"log"
"net/http"
"regexp"
"github.com/gorilla/websocket"
)
var wsClient = []*websocket.Conn{}
var addr = flag.String("port", "58008", "port")
var lastUpdate = []byte("")
var upgrader = &websocket.Upgrader{CheckOrigin: func(r *http.Request) bool{ return true}}
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
wsClient = append(wsClient, c)
defer c.Close()
if len(lastUpdate) > 3{
log.Printf("start: %s", lastUpdate)
c.WriteMessage(websocket.TextMessage, lastUpdate)
}
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
matched, _ := regexp.MatchString("updateChannelClients", string(message[:]))
if matched{
lastUpdate = message
log.Println("info: lastUpdate")
}
if wsClient != nil{
for i, client := range wsClient{
err = client.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
wsClient = append(wsClient[:i], wsClient[i+1:]...)
client.Close()
break
}
}
}
}
}
func main() {
flag.Parse()
log.SetFlags(0)
fs := http.FileServer(http.Dir("./Web-Overlay/"))
cache := http.StripPrefix("/cache/", http.FileServer(http.Dir("./../../../../cache/")))
http.Handle("/", fs)
http.Handle("/cache/", cache)
http.HandleFunc("/ws", echo)
log.Fatal(http.ListenAndServe("localhost:" + *addr, nil))
}