2014-04-19 01:25:11 +04:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
2013-10-16 20:41:47 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-06-07 02:20:22 +03:00
|
|
|
"bytes"
|
2013-10-16 20:41:47 +04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2016-06-07 02:20:22 +03:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2013-10-16 20:41:47 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Time allowed to write a message to the peer.
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
|
|
|
pongWait = 60 * time.Second
|
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = (pongWait * 9) / 10
|
|
|
|
|
|
|
|
// Maximum message size allowed from peer.
|
|
|
|
maxMessageSize = 512
|
|
|
|
)
|
|
|
|
|
2016-06-07 02:20:22 +03:00
|
|
|
var (
|
|
|
|
newline = []byte{'\n'}
|
|
|
|
space = []byte{' '}
|
|
|
|
)
|
|
|
|
|
2014-04-18 02:13:15 +04:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
|
2016-07-19 02:08:34 +03:00
|
|
|
// Client is an middleman between the websocket connection and the hub.
|
|
|
|
type Client struct {
|
|
|
|
hub *Hub
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// The websocket connection.
|
2016-07-19 02:08:34 +03:00
|
|
|
conn *websocket.Conn
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
// Buffered channel of outbound messages.
|
|
|
|
send chan []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// readPump pumps messages from the websocket connection to the hub.
|
2016-10-26 02:13:36 +03:00
|
|
|
//
|
|
|
|
// The application runs readPump in a per-connection goroutine. The application
|
|
|
|
// ensures that there is at most one reader on a connection by executing all
|
|
|
|
// reads from this goroutine.
|
2016-07-19 02:08:34 +03:00
|
|
|
func (c *Client) readPump() {
|
2013-10-16 20:41:47 +04:00
|
|
|
defer func() {
|
2016-07-19 02:08:34 +03:00
|
|
|
c.hub.unregister <- c
|
|
|
|
c.conn.Close()
|
2013-10-16 20:41:47 +04:00
|
|
|
}()
|
2016-07-19 02:08:34 +03:00
|
|
|
c.conn.SetReadLimit(maxMessageSize)
|
|
|
|
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
2013-10-16 20:41:47 +04:00
|
|
|
for {
|
2016-07-19 02:08:34 +03:00
|
|
|
_, message, err := c.conn.ReadMessage()
|
2013-10-16 20:41:47 +04:00
|
|
|
if err != nil {
|
2016-01-20 05:24:46 +03:00
|
|
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
|
|
|
log.Printf("error: %v", err)
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
break
|
|
|
|
}
|
2016-06-07 02:20:22 +03:00
|
|
|
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
|
2016-07-19 02:08:34 +03:00
|
|
|
c.hub.broadcast <- message
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writePump pumps messages from the hub to the websocket connection.
|
2016-10-26 02:13:36 +03:00
|
|
|
//
|
|
|
|
// A goroutine running writePump is started for each connection. The
|
|
|
|
// application ensures that there is at most one writer to a connection by
|
|
|
|
// executing all writes from this goroutine.
|
2016-07-19 02:08:34 +03:00
|
|
|
func (c *Client) writePump() {
|
2013-10-16 20:41:47 +04:00
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
2016-07-19 02:08:34 +03:00
|
|
|
c.conn.Close()
|
2013-10-16 20:41:47 +04:00
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
2016-05-27 07:00:24 +03:00
|
|
|
case message, ok := <-c.send:
|
2016-10-26 02:13:36 +03:00
|
|
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
2013-10-16 20:41:47 +04:00
|
|
|
if !ok {
|
2016-06-07 02:20:22 +03:00
|
|
|
// The hub closed the channel.
|
2016-10-26 02:13:36 +03:00
|
|
|
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
2016-06-07 02:20:22 +03:00
|
|
|
|
2016-07-19 02:08:34 +03:00
|
|
|
w, err := c.conn.NextWriter(websocket.TextMessage)
|
2016-06-07 02:20:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(message)
|
|
|
|
|
|
|
|
// Add queued chat messages to the current websocket message.
|
|
|
|
n := len(c.send)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
w.Write(newline)
|
|
|
|
w.Write(<-c.send)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Close(); err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
2016-10-26 02:13:36 +03:00
|
|
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
if err := c.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 21:40:04 +03:00
|
|
|
// serveWs handles websocket requests from the peer.
|
2016-07-19 02:08:34 +03:00
|
|
|
func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
2014-04-16 19:42:15 +04:00
|
|
|
if err != nil {
|
2014-06-28 00:08:22 +04:00
|
|
|
log.Println(err)
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
2016-07-19 02:08:34 +03:00
|
|
|
client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
|
|
|
|
client.hub.register <- client
|
|
|
|
go client.writePump()
|
|
|
|
client.readPump()
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|