2017-10-25 18:42:44 +02:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2017-10-27 19:40:42 +02:00
|
|
|
"github.com/google/uuid"
|
2017-10-25 18:42:44 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
const channelBufSize = 1000
|
2017-10-25 18:42:44 +02:00
|
|
|
|
2018-08-23 21:02:51 +02:00
|
|
|
// Client of Websocket Server Connection
|
2017-10-25 18:42:44 +02:00
|
|
|
type Client struct {
|
2017-10-27 19:40:42 +02:00
|
|
|
id uuid.UUID
|
2017-10-25 18:42:44 +02:00
|
|
|
server *Server
|
|
|
|
ws *websocket.Conn
|
|
|
|
out chan *Message
|
|
|
|
writeQuit chan bool
|
|
|
|
readQuit chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(s *Server, ws *websocket.Conn) *Client {
|
|
|
|
|
|
|
|
if ws == nil {
|
|
|
|
log.Panic("ws cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
server: s,
|
|
|
|
ws: ws,
|
2017-10-27 19:40:42 +02:00
|
|
|
id: uuid.New(), // fallback id (for testing)
|
2017-10-25 18:42:44 +02:00
|
|
|
out: make(chan *Message, channelBufSize),
|
|
|
|
writeQuit: make(chan bool),
|
|
|
|
readQuit: make(chan bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetID() string {
|
2017-10-27 19:40:42 +02:00
|
|
|
if c.ws != nil {
|
|
|
|
return c.ws.RemoteAddr().String()
|
|
|
|
}
|
|
|
|
return c.id.String()
|
2017-10-25 18:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Write(msg *Message) {
|
|
|
|
select {
|
|
|
|
case c.out <- msg:
|
|
|
|
default:
|
|
|
|
c.server.DelClient(c)
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Close() {
|
|
|
|
c.writeQuit <- true
|
|
|
|
c.readQuit <- true
|
|
|
|
log.Info("client disconnecting...", c.GetID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen Write and Read request via channel
|
|
|
|
func (c *Client) Listen() {
|
|
|
|
go c.listenWrite()
|
|
|
|
c.server.AddClient(c)
|
|
|
|
c.listenRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) handleInput(msg *Message) {
|
|
|
|
msg.From = c
|
2017-10-27 19:40:42 +02:00
|
|
|
if sm := c.server.sessionManager; sm != nil && sm.HandleMessage(msg) {
|
2017-10-25 18:42:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok, err := msg.Validate(); ok {
|
|
|
|
c.server.msgChanIn <- msg
|
|
|
|
} else {
|
|
|
|
log.Println("no valid msg for:", c.GetID(), "error:", err, "\nmessage:", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen write request via channel
|
|
|
|
func (c *Client) listenWrite() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-c.out:
|
|
|
|
websocket.WriteJSON(c.ws, msg)
|
|
|
|
|
|
|
|
case <-c.writeQuit:
|
|
|
|
c.server.DelClient(c)
|
|
|
|
close(c.out)
|
|
|
|
close(c.writeQuit)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen read request via channel
|
|
|
|
func (c *Client) listenRead() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-c.readQuit:
|
|
|
|
c.server.DelClient(c)
|
|
|
|
close(c.readQuit)
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
var msg Message
|
|
|
|
err := websocket.ReadJSON(c.ws, &msg)
|
2017-11-17 20:38:53 +01:00
|
|
|
if websocket.IsCloseError(err, websocket.CloseGoingAway) {
|
2017-10-25 18:42:44 +02:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2017-11-17 20:38:53 +01:00
|
|
|
log.Infof("error on reading %s: %s", c.GetID(), err)
|
|
|
|
return
|
2017-10-25 18:42:44 +02:00
|
|
|
} else {
|
|
|
|
c.handleInput(&msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|