forked from mirror/websocket
Use single character receiver names in chat example
This commit is contained in:
parent
be95f72b73
commit
24cddddcc0
|
@ -30,8 +30,8 @@ var upgrader = websocket.Upgrader{
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
}
|
}
|
||||||
|
|
||||||
// connection is an middleman between the websocket connection and the hub.
|
// Conn is an middleman between the websocket connection and the hub.
|
||||||
type connection struct {
|
type Conn struct {
|
||||||
// The websocket connection.
|
// The websocket connection.
|
||||||
ws *websocket.Conn
|
ws *websocket.Conn
|
||||||
|
|
||||||
|
@ -40,51 +40,51 @@ type connection struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// readPump pumps messages from the websocket connection to the hub.
|
// readPump pumps messages from the websocket connection to the hub.
|
||||||
func (conn *connection) readPump() {
|
func (c *Conn) readPump() {
|
||||||
defer func() {
|
defer func() {
|
||||||
mainHub.unregister <- conn
|
hub.unregister <- c
|
||||||
conn.ws.Close()
|
c.ws.Close()
|
||||||
}()
|
}()
|
||||||
conn.ws.SetReadLimit(maxMessageSize)
|
c.ws.SetReadLimit(maxMessageSize)
|
||||||
conn.ws.SetReadDeadline(time.Now().Add(pongWait))
|
c.ws.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
conn.ws.SetPongHandler(func(string) error { conn.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
||||||
for {
|
for {
|
||||||
_, message, err := conn.ws.ReadMessage()
|
_, message, err := c.ws.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
||||||
log.Printf("error: %v", err)
|
log.Printf("error: %v", err)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
mainHub.broadcast <- message
|
hub.broadcast <- message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write writes a message with the given message type and payload.
|
// write writes a message with the given message type and payload.
|
||||||
func (conn *connection) write(mt int, payload []byte) error {
|
func (c *Conn) write(mt int, payload []byte) error {
|
||||||
conn.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
return conn.ws.WriteMessage(mt, payload)
|
return c.ws.WriteMessage(mt, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
// writePump pumps messages from the hub to the websocket connection.
|
// writePump pumps messages from the hub to the websocket connection.
|
||||||
func (conn *connection) writePump() {
|
func (c *Conn) writePump() {
|
||||||
ticker := time.NewTicker(pingPeriod)
|
ticker := time.NewTicker(pingPeriod)
|
||||||
defer func() {
|
defer func() {
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
conn.ws.Close()
|
c.ws.Close()
|
||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case message, ok := <-conn.send:
|
case message, ok := <-c.send:
|
||||||
if !ok {
|
if !ok {
|
||||||
conn.write(websocket.CloseMessage, []byte{})
|
c.write(websocket.CloseMessage, []byte{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := conn.write(websocket.TextMessage, message); err != nil {
|
if err := c.write(websocket.TextMessage, message); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if err := conn.write(websocket.PingMessage, []byte{}); err != nil {
|
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,8 +98,8 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn := &connection{send: make(chan []byte, 256), ws: ws}
|
conn := &Conn{send: make(chan []byte, 256), ws: ws}
|
||||||
mainHub.register <- conn
|
hub.register <- conn
|
||||||
go conn.writePump()
|
go conn.writePump()
|
||||||
conn.readPump()
|
conn.readPump()
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,39 +6,39 @@ package main
|
||||||
|
|
||||||
// hub maintains the set of active connections and broadcasts messages to the
|
// hub maintains the set of active connections and broadcasts messages to the
|
||||||
// connections.
|
// connections.
|
||||||
type hub struct {
|
type Hub struct {
|
||||||
// Registered connections.
|
// Registered connections.
|
||||||
connections map[*connection]bool
|
connections map[*Conn]bool
|
||||||
|
|
||||||
// Inbound messages from the connections.
|
// Inbound messages from the connections.
|
||||||
broadcast chan []byte
|
broadcast chan []byte
|
||||||
|
|
||||||
// Register requests from the connections.
|
// Register requests from the connections.
|
||||||
register chan *connection
|
register chan *Conn
|
||||||
|
|
||||||
// Unregister requests from connections.
|
// Unregister requests from connections.
|
||||||
unregister chan *connection
|
unregister chan *Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
var mainHub = hub{
|
var hub = Hub{
|
||||||
broadcast: make(chan []byte),
|
broadcast: make(chan []byte),
|
||||||
register: make(chan *connection),
|
register: make(chan *Conn),
|
||||||
unregister: make(chan *connection),
|
unregister: make(chan *Conn),
|
||||||
connections: make(map[*connection]bool),
|
connections: make(map[*Conn]bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hub *hub) run() {
|
func (h *Hub) run() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case conn := <-hub.register:
|
case conn := <-h.register:
|
||||||
hub.connections[conn] = true
|
h.connections[conn] = true
|
||||||
case conn := <-hub.unregister:
|
case conn := <-h.unregister:
|
||||||
if _, ok := hub.connections[conn]; ok {
|
if _, ok := h.connections[conn]; ok {
|
||||||
delete(hub.connections, conn)
|
delete(h.connections, conn)
|
||||||
close(conn.send)
|
close(conn.send)
|
||||||
}
|
}
|
||||||
case message := <-hub.broadcast:
|
case message := <-h.broadcast:
|
||||||
for conn := range hub.connections {
|
for conn := range h.connections {
|
||||||
select {
|
select {
|
||||||
case conn.send <- message:
|
case conn.send <- message:
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -29,7 +29,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
go mainHub.run()
|
go hub.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