owncast/core/chat/client.go

120 lines
2.2 KiB
Go
Raw Normal View History

package chat
2020-05-24 03:57:49 +03:00
import (
"fmt"
"io"
"time"
2020-05-24 03:57:49 +03:00
2020-06-18 09:01:49 +03:00
log "github.com/sirupsen/logrus"
2020-05-24 03:57:49 +03:00
"golang.org/x/net/websocket"
"github.com/gabek/owncast/models"
"github.com/gabek/owncast/utils"
2020-05-24 03:57:49 +03:00
)
const channelBufSize = 100
//Client represents a chat client.
2020-05-24 03:57:49 +03:00
type Client struct {
ConnectedAt time.Time
MessageCount int
id string
2020-05-24 03:57:49 +03:00
ws *websocket.Conn
ch chan models.ChatMessage
pingch chan models.PingMessage
2020-05-24 03:57:49 +03:00
doneCh chan bool
}
//NewClient creates a new chat client
func NewClient(ws *websocket.Conn) *Client {
2020-05-24 03:57:49 +03:00
if ws == nil {
2020-06-18 09:01:49 +03:00
log.Panicln("ws cannot be nil")
2020-05-24 03:57:49 +03:00
}
ch := make(chan models.ChatMessage, channelBufSize)
2020-05-24 03:57:49 +03:00
doneCh := make(chan bool)
pingch := make(chan models.PingMessage)
clientID := utils.GenerateClientIDFromRequest(ws.Request())
return &Client{time.Now(), 0, clientID, ws, ch, pingch, doneCh}
2020-05-24 03:57:49 +03:00
}
//GetConnection gets the connection for the client
func (c *Client) GetConnection() *websocket.Conn {
2020-05-24 03:57:49 +03:00
return c.ws
}
func (c *Client) Write(msg models.ChatMessage) {
2020-05-24 03:57:49 +03:00
select {
case c.ch <- msg:
default:
_server.remove(c)
_server.err(fmt.Errorf("client %s is disconnected", c.id))
2020-05-24 03:57:49 +03:00
}
}
//Done marks the client as done
2020-05-24 03:57:49 +03:00
func (c *Client) Done() {
c.doneCh <- true
}
// Listen Write and Read request via chanel
func (c *Client) Listen() {
go c.listenWrite()
c.listenRead()
}
// Listen write request via chanel
func (c *Client) listenWrite() {
for {
select {
// Send a PING keepalive
case msg := <-c.pingch:
websocket.JSON.Send(c.ws, msg)
2020-05-24 03:57:49 +03:00
// send message to the client
case msg := <-c.ch:
msg.MessageType = "CHAT"
2020-06-17 05:08:34 +03:00
// log.Println("Send:", msg)
2020-05-24 03:57:49 +03:00
websocket.JSON.Send(c.ws, msg)
// receive done request
case <-c.doneCh:
_server.remove(c)
2020-05-24 03:57:49 +03:00
c.doneCh <- true // for listenRead method
return
}
}
}
// Listen read request via chanel
func (c *Client) listenRead() {
for {
select {
// receive done request
case <-c.doneCh:
_server.remove(c)
2020-05-24 03:57:49 +03:00
c.doneCh <- true // for listenWrite method
return
// read data from websocket connection
default:
var msg models.ChatMessage
if err := websocket.JSON.Receive(c.ws, &msg); err == io.EOF {
2020-05-24 03:57:49 +03:00
c.doneCh <- true
return
2020-05-24 03:57:49 +03:00
} else if err != nil {
_server.err(err)
2020-05-24 03:57:49 +03:00
} else {
c.MessageCount++
msg.ClientID = c.id
_server.SendToAll(msg)
2020-05-24 03:57:49 +03:00
}
}
}
}