2020-06-23 03:11:56 +02:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/net/websocket"
|
2020-06-23 08:52:50 +02:00
|
|
|
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/models"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
var (
|
|
|
|
_server *server
|
|
|
|
)
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//Server represents the server which handles the chat
|
2020-06-23 22:11:01 +02:00
|
|
|
type server struct {
|
2020-08-06 06:01:06 +02:00
|
|
|
Clients map[string]*Client
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
pattern string
|
|
|
|
listener models.ChatListener
|
|
|
|
|
2020-06-23 03:11:56 +02: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 22:11:01 +02:00
|
|
|
func (s *server) add(c *Client) {
|
2020-06-23 03:11:56 +02:00
|
|
|
s.addCh <- c
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove removes a client from the server
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) remove(c *Client) {
|
2020-06-23 03:11:56 +02:00
|
|
|
s.delCh <- c
|
|
|
|
}
|
|
|
|
|
|
|
|
//SendToAll sends a message to all of the connected clients
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) SendToAll(msg models.ChatMessage) {
|
2020-06-23 03:11:56 +02:00
|
|
|
s.sendAllCh <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
//Done marks the server as done
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) done() {
|
2020-06-23 03:11:56 +02:00
|
|
|
s.doneCh <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
//Err handles an error
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) err(err error) {
|
2020-06-23 03:11:56 +02:00
|
|
|
s.errCh <- err
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) sendAll(msg models.ChatMessage) {
|
2020-06-23 03:11:56 +02:00
|
|
|
for _, c := range s.Clients {
|
|
|
|
c.Write(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) ping() {
|
2020-07-29 06:30:03 +02:00
|
|
|
ping := models.PingMessage{MessageType: PING}
|
2020-06-23 03:11:56 +02:00
|
|
|
for _, c := range s.Clients {
|
|
|
|
c.pingch <- ping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 06:30:03 +02:00
|
|
|
func (s *server) usernameChanged(msg models.NameChangeEvent) {
|
|
|
|
for _, c := range s.Clients {
|
|
|
|
c.usernameChangeChannel <- msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) onConnection(ws *websocket.Conn) {
|
|
|
|
client := NewClient(ws)
|
2020-06-23 08:52:50 +02:00
|
|
|
|
|
|
|
defer func() {
|
2020-10-07 08:14:33 +02:00
|
|
|
log.Tracef("The client was connected for %s and sent %d messages (%s)", time.Since(client.ConnectedAt), client.MessageCount, client.ClientID)
|
2020-06-23 08:52:50 +02:00
|
|
|
|
|
|
|
if err := ws.Close(); err != nil {
|
|
|
|
s.errCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
s.add(client)
|
2020-06-23 08:52:50 +02:00
|
|
|
client.Listen()
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
// Listen and serve.
|
|
|
|
// It serves client connection and broadcast request.
|
2020-06-23 22:11:01 +02:00
|
|
|
func (s *server) Listen() {
|
2020-06-23 08:52:50 +02:00
|
|
|
http.Handle(s.pattern, websocket.Handler(s.onConnection))
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-07 06:27:31 +02:00
|
|
|
log.Tracef("Starting the websocket listener on: %s", s.pattern)
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// add new a client
|
|
|
|
case c := <-s.addCh:
|
2020-07-29 06:30:03 +02:00
|
|
|
s.Clients[c.socketID] = c
|
2020-10-07 08:14:33 +02:00
|
|
|
s.listener.ClientAdded(c.GetViewerClientFromChatClient())
|
2020-07-20 00:14:51 +02:00
|
|
|
s.sendWelcomeMessageToClient(c)
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
// remove a client
|
|
|
|
case c := <-s.delCh:
|
2020-07-29 06:30:03 +02:00
|
|
|
delete(s.Clients, c.socketID)
|
2020-10-07 08:14:33 +02:00
|
|
|
s.listener.ClientRemoved(c.ClientID)
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-10-14 01:45:52 +02:00
|
|
|
// message was recieved from a client and should be sanitized, validated
|
|
|
|
// and distributed to other clients.
|
2020-06-23 03:11:56 +02:00
|
|
|
case msg := <-s.sendAllCh:
|
2020-10-14 01:45:52 +02:00
|
|
|
// Will turn markdown into html, sanitize user-supplied raw html
|
|
|
|
// and standardize this message into something safe we can send everyone else.
|
|
|
|
msg.RenderAndSanitizeMessageBody()
|
|
|
|
|
2020-06-23 22:11:01 +02:00
|
|
|
s.listener.MessageSent(msg)
|
2020-06-23 03:11:56 +02:00
|
|
|
s.sendAll(msg)
|
2020-10-14 01:45:52 +02:00
|
|
|
|
|
|
|
// Store in the message history
|
2020-07-12 23:53:33 +02:00
|
|
|
addMessage(msg)
|
2020-06-23 03:11:56 +02:00
|
|
|
case ping := <-s.pingCh:
|
|
|
|
fmt.Println("PING?", ping)
|
|
|
|
|
|
|
|
case err := <-s.errCh:
|
2020-07-07 06:27:31 +02:00
|
|
|
log.Error("Error:", err.Error())
|
2020-06-23 03:11:56 +02:00
|
|
|
|
|
|
|
case <-s.doneCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 00:14:51 +02:00
|
|
|
|
|
|
|
func (s *server) sendWelcomeMessageToClient(c *Client) {
|
|
|
|
go func() {
|
2020-07-20 09:22:32 +02:00
|
|
|
// Add an artificial delay so people notice this message come in.
|
|
|
|
time.Sleep(7 * time.Second)
|
2020-07-20 00:14:51 +02:00
|
|
|
|
|
|
|
initialChatMessageText := fmt.Sprintf("Welcome to %s! %s", config.Config.InstanceDetails.Title, config.Config.InstanceDetails.Summary)
|
2020-10-02 08:55:38 +02:00
|
|
|
initialMessage := models.ChatMessage{"owncast-server", config.Config.InstanceDetails.Name, initialChatMessageText, config.Config.InstanceDetails.Logo.Small, "initial-message-1", "CHAT", true, time.Now()}
|
2020-07-20 00:14:51 +02:00
|
|
|
c.Write(initialMessage)
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
2020-10-07 08:14:33 +02:00
|
|
|
|
|
|
|
func (s *server) getClientForClientID(clientID string) *Client {
|
|
|
|
for _, client := range s.Clients {
|
|
|
|
if client.ClientID == clientID {
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|