2020-06-23 04:11:56 +03:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/net/websocket"
|
2020-06-23 09:52:50 +03:00
|
|
|
|
|
|
|
"github.com/gabek/owncast/models"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
var (
|
|
|
|
_server *server
|
|
|
|
)
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//Server represents the server which handles the chat
|
2020-06-23 23:11:01 +03:00
|
|
|
type server struct {
|
2020-06-23 04:11:56 +03:00
|
|
|
Messages []models.ChatMessage
|
|
|
|
Clients map[string]*Client
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
pattern string
|
|
|
|
listener models.ChatListener
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
addCh chan *Client
|
|
|
|
delCh chan *Client
|
|
|
|
sendAllCh chan models.ChatMessage
|
|
|
|
pingCh chan models.PingMessage
|
|
|
|
doneCh chan bool
|
|
|
|
errCh chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add adds a client to the server
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) add(c *Client) {
|
2020-06-23 04:11:56 +03:00
|
|
|
s.addCh <- c
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove removes a client from the server
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) remove(c *Client) {
|
2020-06-23 04:11:56 +03:00
|
|
|
s.delCh <- c
|
|
|
|
}
|
|
|
|
|
|
|
|
//SendToAll sends a message to all of the connected clients
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) SendToAll(msg models.ChatMessage) {
|
2020-06-23 04:11:56 +03:00
|
|
|
s.sendAllCh <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
//Done marks the server as done
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) done() {
|
2020-06-23 04:11:56 +03:00
|
|
|
s.doneCh <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
//Err handles an error
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) err(err error) {
|
2020-06-23 04:11:56 +03:00
|
|
|
s.errCh <- err
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) sendPastMessages(c *Client) {
|
2020-06-23 04:11:56 +03:00
|
|
|
for _, msg := range s.Messages {
|
|
|
|
c.Write(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) sendAll(msg models.ChatMessage) {
|
2020-06-23 04:11:56 +03:00
|
|
|
for _, c := range s.Clients {
|
|
|
|
c.Write(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) ping() {
|
2020-06-23 04:11:56 +03:00
|
|
|
ping := models.PingMessage{MessageType: "PING"}
|
|
|
|
for _, c := range s.Clients {
|
|
|
|
c.pingch <- ping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) onConnection(ws *websocket.Conn) {
|
|
|
|
client := NewClient(ws)
|
2020-06-23 09:52:50 +03:00
|
|
|
|
|
|
|
defer func() {
|
2020-07-07 08:24:55 +03:00
|
|
|
log.Trace("The client was connected for %s and sent %d messages (%s)", time.Since(client.ConnectedAt), client.MessageCount, client.id)
|
2020-06-23 09:52:50 +03:00
|
|
|
|
|
|
|
if err := ws.Close(); err != nil {
|
|
|
|
s.errCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
s.add(client)
|
2020-06-23 09:52:50 +03:00
|
|
|
client.Listen()
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
// Listen and serve.
|
|
|
|
// It serves client connection and broadcast request.
|
2020-06-23 23:11:01 +03:00
|
|
|
func (s *server) Listen() {
|
2020-06-23 09:52:50 +03:00
|
|
|
http.Handle(s.pattern, websocket.Handler(s.onConnection))
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Tracef("Starting the websocket listener on: %s", s.pattern)
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// add new a client
|
|
|
|
case c := <-s.addCh:
|
|
|
|
s.Clients[c.id] = c
|
|
|
|
|
2020-06-23 23:11:01 +03:00
|
|
|
s.listener.ClientAdded(c.id)
|
2020-06-23 04:11:56 +03:00
|
|
|
s.sendPastMessages(c)
|
|
|
|
|
|
|
|
// remove a client
|
|
|
|
case c := <-s.delCh:
|
|
|
|
delete(s.Clients, c.id)
|
2020-06-23 23:11:01 +03:00
|
|
|
s.listener.ClientRemoved(c.id)
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
// broadcast a message to all clients
|
|
|
|
case msg := <-s.sendAllCh:
|
|
|
|
s.Messages = append(s.Messages, msg)
|
2020-06-23 23:11:01 +03:00
|
|
|
s.listener.MessageSent(msg)
|
2020-06-23 04:11:56 +03:00
|
|
|
s.sendAll(msg)
|
|
|
|
|
|
|
|
case ping := <-s.pingCh:
|
|
|
|
fmt.Println("PING?", ping)
|
|
|
|
|
|
|
|
case err := <-s.errCh:
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Error("Error:", err.Error())
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
case <-s.doneCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|