golang-lib/websocket/client.go

123 lines
2.5 KiB
Go
Raw Normal View History

2017-10-25 18:42:44 +02:00
package websocket
import (
2017-10-27 19:40:42 +02:00
"github.com/google/uuid"
2019-01-13 22:36:40 +01:00
"github.com/bdlm/log"
2017-10-25 18:42:44 +02:00
"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
}
// NewClient by websocket
2017-10-25 18:42:44 +02:00
func NewClient(s *Server, ws *websocket.Conn) *Client {
if ws == nil {
2019-01-13 22:36:40 +01:00
log.WithField("modul", "websocket").Panic("client cannot be created without websocket")
2017-10-25 18:42:44 +02:00
}
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),
}
}
// GetID of Client ( UUID or Address to Client)
2017-10-25 18:42:44 +02:00
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
}
// Write Message to Client
2017-10-25 18:42:44 +02:00
func (c *Client) Write(msg *Message) {
select {
case c.out <- msg:
default:
c.server.delClient(c)
2017-10-25 18:42:44 +02:00
c.Close()
}
}
// Close Client
2017-10-25 18:42:44 +02:00
func (c *Client) Close() {
c.writeQuit <- true
c.readQuit <- true
2019-01-13 22:36:40 +01:00
log.WithField("modul", "websocket").Info("client disconnecting...", c.GetID())
2017-10-25 18:42:44 +02:00
}
// Listen write and read request via channel
2017-10-25 18:42:44 +02:00
func (c *Client) Listen() {
go c.listenWrite()
c.server.addClient(c)
2017-10-25 18:42:44 +02:00
c.listenRead()
}
// handleInput manage session and valide message before send to server
2017-10-25 18:42:44 +02:00
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 {
msg.server = c.server
2017-10-25 18:42:44 +02:00
c.server.msgChanIn <- msg
} else {
2019-01-13 22:36:40 +01:00
log.WithField("modul", "websocket").Println("no valid msg for:", c.GetID(), "error:", err, "\nmessage:", msg)
2017-10-25 18:42:44 +02:00
}
}
// listenWrite request via channel
2017-10-25 18:42:44 +02:00
func (c *Client) listenWrite() {
for {
select {
case msg := <-c.out:
websocket.WriteJSON(c.ws, msg)
case <-c.writeQuit:
c.server.delClient(c)
2017-10-25 18:42:44 +02:00
close(c.out)
close(c.writeQuit)
return
}
}
}
// listenRead request via channel
2017-10-25 18:42:44 +02:00
func (c *Client) listenRead() {
for {
select {
case <-c.readQuit:
c.server.delClient(c)
2017-10-25 18:42:44 +02:00
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 {
2019-01-13 22:36:40 +01:00
log.WithField("modul", "websocket").Warnf("error on reading %s: %s", c.GetID(), err)
2017-11-17 20:38:53 +01:00
return
2017-10-25 18:42:44 +02:00
} else {
c.handleInput(&msg)
}
}
}
}