Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
Fix a memory leak with the chat aspect. (#23)
Bradley Hilton1fecd240bdd3
diff --git a/core/chat/client.go b/core/chat/client.goindex 6dab147f08..15bf0ea164 100644--- a/core/chat/client.go+++ b/core/chat/client.go@@ -3,6 +3,7 @@ package chatimport ("fmt""io"+ "time"log "github.com/sirupsen/logrus""golang.org/x/net/websocket"@@ -15,6 +16,9 @@ const channelBufSize = 100//Client represents a chat client.type Client struct {+ ConnectedAt time.Time+ MessageCount int+id stringws *websocket.Connserver *Server@@ -39,7 +43,7 @@ func NewClient(ws *websocket.Conn, server *Server) *Client {pingch := make(chan models.PingMessage)clientID := utils.GenerateClientIDFromRequest(ws.Request())- return &Client{clientID, ws, server, ch, pingch, doneCh}+ return &Client{time.Now(), 0, clientID, ws, server, ch, pingch, doneCh}}//GetConnection gets the connection for the client@@ -106,9 +110,11 @@ func (c *Client) listenRead() {if err := websocket.JSON.Receive(c.ws, &msg); err == io.EOF {c.doneCh <- true+ return} else if err != nil {c.server.Err(err)} else {+ c.MessageCount++c.server.SendToAll(msg)}}diff --git a/core/chat/server.go b/core/chat/server.goindex f4b1ddde70..466794181c 100644--- a/core/chat/server.go+++ b/core/chat/server.go@@ -5,19 +5,19 @@ import ("net/http""time"- "github.com/gabek/owncast/core"- "github.com/gabek/owncast/models"log "github.com/sirupsen/logrus"-"golang.org/x/net/websocket"++ "github.com/gabek/owncast/core"+ "github.com/gabek/owncast/models")//Server represents the server which handles the chattype Server struct {- Pattern stringMessages []models.ChatMessageClients map[string]*Client+ pattern stringaddCh chan *ClientdelCh chan *ClientsendAllCh chan models.ChatMessage@@ -27,7 +27,7 @@ type Server struct {}//NewServer creates a new chat server-func NewServer(pattern string) *Server {+func NewServer() *Server {messages := []models.ChatMessage{}clients := make(map[string]*Client)addCh := make(chan *Client)@@ -46,9 +46,9 @@ func NewServer(pattern string) *Server {messages = append(messages, models.ChatMessage{"Blathers", "Blathers is an owl with brown feathers. His face is white and he has a yellow beak. His arms are wing shaped and he has yellow talons. His eyes are very big with small black irises. He also has big pink cheek circles on his cheeks. His belly appears to be checkered in diamonds with light brown and white squares, similar to an argyle vest, which is traditionally associated with academia. His green bowtie further alludes to his academic nature.", "https://vignette.wikia.nocookie.net/animalcrossing/images/b/b3/NH-character-Blathers.png/revision/latest?cb=20200229053519", "demo-message-6", "ChatMessage"})server := &Server{- pattern,messages,clients,+ "/entry", //hardcoded due to the UI requiring this and it is not configurableaddCh,delCh,sendAllCh,@@ -116,26 +116,27 @@ func (s *Server) ping() {}}+func (s *Server) onConnection(ws *websocket.Conn) {+ client := NewClient(ws, s)++ defer func() {+ log.Printf("The client was connected for %s and sent %d messages (%s)", time.Since(client.ConnectedAt), client.MessageCount, client.id)++ if err := ws.Close(); err != nil {+ s.errCh <- err+ }+ }()++ s.Add(client)+ client.Listen()+}+// Listen and serve.// It serves client connection and broadcast request.func (s *Server) Listen() {- // websocket handler- onConnected := func(ws *websocket.Conn) {- defer func() {- err := ws.Close()- if err != nil {- s.errCh <- err- }- }()-- client := NewClient(ws, s)- s.Add(client)- client.Listen()- }-- http.Handle(s.Pattern, websocket.Handler(onConnected))+ http.Handle(s.pattern, websocket.Handler(s.onConnection))- log.Printf("Starting the websocket listener on: %s", s.Pattern)+ log.Printf("Starting the websocket listener on: %s", s.pattern)for {select {diff --git a/router/router.go b/router/router.goindex fbec05b305..039805a501 100644--- a/router/router.go+++ b/router/router.go@@ -15,7 +15,7 @@ import (//Start starts the router for the http, ws, and rtmpfunc Start() error {// websocket server- chatServer := chat.NewServer("/entry")+ chatServer := chat.NewServer()go chatServer.Listen()// start the rtmp serverdiff --git a/webroot/js/app.js b/webroot/js/app.jsindex e7f334f887..95b9738eda 100644--- a/webroot/js/app.js+++ b/webroot/js/app.js@@ -66,9 +66,9 @@ function setupWebsocket() {// Uncomment to point to somewhere other than goth.landconst protocol = location.protocol == "https:" ? "wss" : "ws"- // var ws = new WebSocket(protocol + "://" + location.host + "/entry")+ var ws = new WebSocket(protocol + "://" + location.host + "/entry")- var ws = new WebSocket("wss://goth.land/entry")+ // var ws = new WebSocket("wss://goth.land/entry")ws.onmessage = (e) => {const model = JSON.parse(e.data)