[Examples\Chat] Allow to free HTTP conn things

This commit is contained in:
Dmitry Patsura 2017-06-13 00:40:37 +09:00 committed by GitHub
parent a91eba7f97
commit d5c5d34e8f
1 changed files with 9 additions and 2 deletions

View File

@ -120,6 +120,11 @@ func (c *Client) writePump() {
}
}
func handleClientWSConnection(client *Client) {
go client.writePump()
client.readPump()
}
// serveWs handles websocket requests from the peer.
func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
@ -127,8 +132,10 @@ func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
log.Println(err)
return
}
client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
client.hub.register <- client
go client.writePump()
client.readPump()
// launch a new goroutine, now HTTP server can free unneeded things
go handleClientWSConnection(client)
}