mirror of https://github.com/gorilla/websocket.git
Meaningful names for hub
This commit is contained in:
parent
1f512fc3f0
commit
a6108176e8
|
@ -20,3 +20,6 @@ _cgo_export.*
|
||||||
_testmain.go
|
_testmain.go
|
||||||
|
|
||||||
*.exe
|
*.exe
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
*.iml
|
|
@ -42,7 +42,7 @@ type connection struct {
|
||||||
// readPump pumps messages from the websocket connection to the hub.
|
// readPump pumps messages from the websocket connection to the hub.
|
||||||
func (c *connection) readPump() {
|
func (c *connection) readPump() {
|
||||||
defer func() {
|
defer func() {
|
||||||
h.unregister <- c
|
mainHub.unregister <- c
|
||||||
c.ws.Close()
|
c.ws.Close()
|
||||||
}()
|
}()
|
||||||
c.ws.SetReadLimit(maxMessageSize)
|
c.ws.SetReadLimit(maxMessageSize)
|
||||||
|
@ -56,7 +56,7 @@ func (c *connection) readPump() {
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
h.broadcast <- message
|
mainHub.broadcast <- message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c := &connection{send: make(chan []byte, 256), ws: ws}
|
c := &connection{send: make(chan []byte, 256), ws: ws}
|
||||||
h.register <- c
|
mainHub.register <- c
|
||||||
go c.writePump()
|
go c.writePump()
|
||||||
c.readPump()
|
c.readPump()
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,30 +20,30 @@ type hub struct {
|
||||||
unregister chan *connection
|
unregister chan *connection
|
||||||
}
|
}
|
||||||
|
|
||||||
var h = hub{
|
var mainHub = hub{
|
||||||
broadcast: make(chan []byte),
|
broadcast: make(chan []byte),
|
||||||
register: make(chan *connection),
|
register: make(chan *connection),
|
||||||
unregister: make(chan *connection),
|
unregister: make(chan *connection),
|
||||||
connections: make(map[*connection]bool),
|
connections: make(map[*connection]bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hub) run() {
|
func (hub *hub) run() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case c := <-h.register:
|
case c := <-hub.register:
|
||||||
h.connections[c] = true
|
hub.connections[c] = true
|
||||||
case c := <-h.unregister:
|
case c := <-hub.unregister:
|
||||||
if _, ok := h.connections[c]; ok {
|
if _, ok := hub.connections[c]; ok {
|
||||||
delete(h.connections, c)
|
delete(hub.connections, c)
|
||||||
close(c.send)
|
close(c.send)
|
||||||
}
|
}
|
||||||
case m := <-h.broadcast:
|
case m := <-hub.broadcast:
|
||||||
for c := range h.connections {
|
for c := range hub.connections {
|
||||||
select {
|
select {
|
||||||
case c.send <- m:
|
case c.send <- m:
|
||||||
default:
|
default:
|
||||||
close(c.send)
|
close(c.send)
|
||||||
delete(h.connections, c)
|
delete(hub.connections, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var addr = flag.String("addr", ":8080", "http service address")
|
var addr = flag.String("addr", ":8080", "http service address")
|
||||||
var homeTempl = template.Must(template.ParseFiles("home.html"))
|
var homeTemplate = template.Must(template.ParseFiles("home.html"))
|
||||||
|
|
||||||
func serveHome(w http.ResponseWriter, r *http.Request) {
|
func serveHome(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
|
@ -24,12 +24,12 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
homeTempl.Execute(w, r.Host)
|
homeTemplate.Execute(w, r.Host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
go h.run()
|
go mainHub.run()
|
||||||
http.HandleFunc("/", serveHome)
|
http.HandleFunc("/", serveHome)
|
||||||
http.HandleFunc("/ws", serveWs)
|
http.HandleFunc("/ws", serveWs)
|
||||||
err := http.ListenAndServe(*addr, nil)
|
err := http.ListenAndServe(*addr, nil)
|
||||||
|
|
Loading…
Reference in New Issue