mirror of https://github.com/gorilla/websocket.git
Merge branch 'varnames'
This commit is contained in:
commit
afffb15196
|
@ -20,3 +20,6 @@ _cgo_export.*
|
|||
_testmain.go
|
||||
|
||||
*.exe
|
||||
|
||||
.idea/
|
||||
*.iml
|
|
@ -30,8 +30,8 @@ var upgrader = websocket.Upgrader{
|
|||
WriteBufferSize: 1024,
|
||||
}
|
||||
|
||||
// connection is an middleman between the websocket connection and the hub.
|
||||
type connection struct {
|
||||
// Conn is an middleman between the websocket connection and the hub.
|
||||
type Conn struct {
|
||||
// The websocket connection.
|
||||
ws *websocket.Conn
|
||||
|
||||
|
@ -40,9 +40,9 @@ type connection struct {
|
|||
}
|
||||
|
||||
// readPump pumps messages from the websocket connection to the hub.
|
||||
func (c *connection) readPump() {
|
||||
func (c *Conn) readPump() {
|
||||
defer func() {
|
||||
h.unregister <- c
|
||||
hub.unregister <- c
|
||||
c.ws.Close()
|
||||
}()
|
||||
c.ws.SetReadLimit(maxMessageSize)
|
||||
|
@ -56,18 +56,18 @@ func (c *connection) readPump() {
|
|||
}
|
||||
break
|
||||
}
|
||||
h.broadcast <- message
|
||||
hub.broadcast <- message
|
||||
}
|
||||
}
|
||||
|
||||
// write writes a message with the given message type and payload.
|
||||
func (c *connection) write(mt int, payload []byte) error {
|
||||
func (c *Conn) write(mt int, payload []byte) error {
|
||||
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
return c.ws.WriteMessage(mt, payload)
|
||||
}
|
||||
|
||||
// writePump pumps messages from the hub to the websocket connection.
|
||||
func (c *connection) writePump() {
|
||||
func (c *Conn) writePump() {
|
||||
ticker := time.NewTicker(pingPeriod)
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
|
@ -98,8 +98,8 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
|
|||
log.Println(err)
|
||||
return
|
||||
}
|
||||
c := &connection{send: make(chan []byte, 256), ws: ws}
|
||||
h.register <- c
|
||||
go c.writePump()
|
||||
c.readPump()
|
||||
conn := &Conn{send: make(chan []byte, 256), ws: ws}
|
||||
hub.register <- conn
|
||||
go conn.writePump()
|
||||
conn.readPump()
|
||||
}
|
||||
|
|
|
@ -6,44 +6,44 @@ package main
|
|||
|
||||
// hub maintains the set of active connections and broadcasts messages to the
|
||||
// connections.
|
||||
type hub struct {
|
||||
type Hub struct {
|
||||
// Registered connections.
|
||||
connections map[*connection]bool
|
||||
connections map[*Conn]bool
|
||||
|
||||
// Inbound messages from the connections.
|
||||
broadcast chan []byte
|
||||
|
||||
// Register requests from the connections.
|
||||
register chan *connection
|
||||
register chan *Conn
|
||||
|
||||
// Unregister requests from connections.
|
||||
unregister chan *connection
|
||||
unregister chan *Conn
|
||||
}
|
||||
|
||||
var h = hub{
|
||||
var hub = Hub{
|
||||
broadcast: make(chan []byte),
|
||||
register: make(chan *connection),
|
||||
unregister: make(chan *connection),
|
||||
connections: make(map[*connection]bool),
|
||||
register: make(chan *Conn),
|
||||
unregister: make(chan *Conn),
|
||||
connections: make(map[*Conn]bool),
|
||||
}
|
||||
|
||||
func (h *hub) run() {
|
||||
func (h *Hub) run() {
|
||||
for {
|
||||
select {
|
||||
case c := <-h.register:
|
||||
h.connections[c] = true
|
||||
case c := <-h.unregister:
|
||||
if _, ok := h.connections[c]; ok {
|
||||
delete(h.connections, c)
|
||||
close(c.send)
|
||||
case conn := <-h.register:
|
||||
h.connections[conn] = true
|
||||
case conn := <-h.unregister:
|
||||
if _, ok := h.connections[conn]; ok {
|
||||
delete(h.connections, conn)
|
||||
close(conn.send)
|
||||
}
|
||||
case m := <-h.broadcast:
|
||||
for c := range h.connections {
|
||||
case message := <-h.broadcast:
|
||||
for conn := range h.connections {
|
||||
select {
|
||||
case c.send <- m:
|
||||
case conn.send <- message:
|
||||
default:
|
||||
close(c.send)
|
||||
delete(h.connections, c)
|
||||
close(conn.send)
|
||||
delete(hub.connections, conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
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) {
|
||||
if r.URL.Path != "/" {
|
||||
|
@ -24,12 +24,12 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
homeTempl.Execute(w, r.Host)
|
||||
homeTemplate.Execute(w, r.Host)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
go h.run()
|
||||
go hub.run()
|
||||
http.HandleFunc("/", serveHome)
|
||||
http.HandleFunc("/ws", serveWs)
|
||||
err := http.ListenAndServe(*addr, nil)
|
||||
|
|
Loading…
Reference in New Issue