mirror of
https://github.com/owncast/owncast.git
synced 2024-11-28 11:09:01 +03:00
Merge branch 'master' into 0614gw-updates
This commit is contained in:
commit
b3dfba9d94
7 changed files with 96 additions and 36 deletions
18
client.go
18
client.go
|
@ -15,7 +15,9 @@ type Client struct {
|
||||||
id string
|
id string
|
||||||
ws *websocket.Conn
|
ws *websocket.Conn
|
||||||
server *Server
|
server *Server
|
||||||
ch chan *Message
|
ch chan *ChatMessage
|
||||||
|
pingch chan *PingMessage
|
||||||
|
|
||||||
doneCh chan bool
|
doneCh chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,17 +32,18 @@ func NewClient(ws *websocket.Conn, server *Server) *Client {
|
||||||
panic("server cannot be nil")
|
panic("server cannot be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
ch := make(chan *Message, channelBufSize)
|
ch := make(chan *ChatMessage, channelBufSize)
|
||||||
doneCh := make(chan bool)
|
doneCh := make(chan bool)
|
||||||
|
pingch := make(chan *PingMessage)
|
||||||
clientID := getClientIDFromRequest(ws.Request())
|
clientID := getClientIDFromRequest(ws.Request())
|
||||||
return &Client{clientID, ws, server, ch, doneCh}
|
return &Client{clientID, ws, server, ch, pingch, doneCh}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Conn() *websocket.Conn {
|
func (c *Client) Conn() *websocket.Conn {
|
||||||
return c.ws
|
return c.ws
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Write(msg *Message) {
|
func (c *Client) Write(msg *ChatMessage) {
|
||||||
select {
|
select {
|
||||||
case c.ch <- msg:
|
case c.ch <- msg:
|
||||||
default:
|
default:
|
||||||
|
@ -64,9 +67,12 @@ func (c *Client) Listen() {
|
||||||
func (c *Client) listenWrite() {
|
func (c *Client) listenWrite() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
// Send a PING keepalive
|
||||||
|
case msg := <-c.pingch:
|
||||||
|
websocket.JSON.Send(c.ws, msg)
|
||||||
// send message to the client
|
// send message to the client
|
||||||
case msg := <-c.ch:
|
case msg := <-c.ch:
|
||||||
|
msg.MessageType = "CHAT"
|
||||||
log.Println("Send:", msg)
|
log.Println("Send:", msg)
|
||||||
websocket.JSON.Send(c.ws, msg)
|
websocket.JSON.Send(c.ws, msg)
|
||||||
|
|
||||||
|
@ -92,7 +98,7 @@ func (c *Client) listenRead() {
|
||||||
|
|
||||||
// read data from websocket connection
|
// read data from websocket connection
|
||||||
default:
|
default:
|
||||||
var msg Message
|
var msg ChatMessage
|
||||||
err := websocket.JSON.Receive(c.ws, &msg)
|
err := websocket.JSON.Receive(c.ws, &msg)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
c.doneCh <- true
|
c.doneCh <- true
|
||||||
|
|
27
message.go
27
message.go
|
@ -1,12 +1,25 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type Message struct {
|
type ChatMessage struct {
|
||||||
Author string `json:"author"`
|
Author string `json:"author"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
Id string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
MessageType string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Message) String() string {
|
func (s *ChatMessage) AsJson() string {
|
||||||
return self.Author + " says " + self.Body
|
return s.Author + " says " + s.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ChatMessage) String() string {
|
||||||
|
return s.Author + " says " + s.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
type PingMessage struct {
|
||||||
|
MessageType string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *PingMessage) AsJson() string {
|
||||||
|
return "{type: \"PING\"}"
|
||||||
}
|
}
|
||||||
|
|
56
server.go
56
server.go
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
@ -10,43 +12,58 @@ import (
|
||||||
// Chat server.
|
// Chat server.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
pattern string
|
pattern string
|
||||||
messages []*Message
|
messages []*ChatMessage
|
||||||
clients map[string]*Client
|
clients map[string]*Client
|
||||||
addCh chan *Client
|
addCh chan *Client
|
||||||
delCh chan *Client
|
delCh chan *Client
|
||||||
sendAllCh chan *Message
|
sendAllCh chan *ChatMessage
|
||||||
|
pingCh chan *PingMessage
|
||||||
doneCh chan bool
|
doneCh chan bool
|
||||||
errCh chan error
|
errCh chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new chat server.
|
// Create new chat server.
|
||||||
func NewServer(pattern string) *Server {
|
func NewServer(pattern string) *Server {
|
||||||
messages := []*Message{}
|
messages := []*ChatMessage{}
|
||||||
clients := make(map[string]*Client)
|
clients := make(map[string]*Client)
|
||||||
addCh := make(chan *Client)
|
addCh := make(chan *Client)
|
||||||
delCh := make(chan *Client)
|
delCh := make(chan *Client)
|
||||||
sendAllCh := make(chan *Message)
|
sendAllCh := make(chan *ChatMessage)
|
||||||
|
pingCh := make(chan *PingMessage)
|
||||||
doneCh := make(chan bool)
|
doneCh := make(chan bool)
|
||||||
errCh := make(chan error)
|
errCh := make(chan error)
|
||||||
|
|
||||||
// Demo messages only. Remove me eventually!!!
|
// Demo messages only. Remove me eventually!!!
|
||||||
messages = append(messages, &Message{"Tom Nook", "I'll be there with Bells on! Ho ho!", "https://gamepedia.cursecdn.com/animalcrossingpocketcamp_gamepedia_en/thumb/4/4f/Timmy_Icon.png/120px-Timmy_Icon.png?version=87b38d7d6130411d113486c2db151385", "demo-message-1"})
|
messages = append(messages, &ChatMessage{"Tom Nook", "I'll be there with Bells on! Ho ho!", "https://gamepedia.cursecdn.com/animalcrossingpocketcamp_gamepedia_en/thumb/4/4f/Timmy_Icon.png/120px-Timmy_Icon.png?version=87b38d7d6130411d113486c2db151385", "demo-message-1", "ChatMessage"})
|
||||||
messages = append(messages, &Message{"Redd", "Fool me once, shame on you. Fool me twice, stop foolin' me.", "https://vignette.wikia.nocookie.net/animalcrossing/images/3/3d/Redd2.gif/revision/latest?cb=20100710004252", "demo-message-2"})
|
messages = append(messages, &ChatMessage{"Redd", "Fool me once, shame on you. Fool me twice, stop foolin' me.", "https://vignette.wikia.nocookie.net/animalcrossing/images/3/3d/Redd2.gif/revision/latest?cb=20100710004252", "demo-message-2", "ChatMessage"})
|
||||||
messages = append(messages, &Message{"Kevin", "You just caught me before I was about to go work out weeweewee!", "https://vignette.wikia.nocookie.net/animalcrossing/images/2/20/NH-Kevin_poster.png/revision/latest/scale-to-width-down/100?cb=20200410185817", "demo-message-3"})
|
messages = append(messages, &ChatMessage{"Kevin", "You just caught me before I was about to go work out weeweewee!", "https://vignette.wikia.nocookie.net/animalcrossing/images/2/20/NH-Kevin_poster.png/revision/latest/scale-to-width-down/100?cb=20200410185817", "demo-message-3", "ChatMessage"})
|
||||||
messages = append(messages, &Message{"Isabelle", " Isabelle is the mayor's highly capable secretary. She can be forgetful sometimes, but you can always count on her for information about the town. She wears her hair up in a bun that makes her look like a shih tzu. Mostly because she is one! She also has a twin brother named Digby.", "https://dodo.ac/np/images/thumb/7/7b/IsabelleTrophyWiiU.png/200px-IsabelleTrophyWiiU.png", "demo-message-4"})
|
messages = append(messages, &ChatMessage{"Isabelle", " Isabelle is the mayor's highly capable secretary. She can be forgetful sometimes, but you can always count on her for information about the town. She wears her hair up in a bun that makes her look like a shih tzu. Mostly because she is one! She also has a twin brother named Digby.", "https://dodo.ac/np/images/thumb/7/7b/IsabelleTrophyWiiU.png/200px-IsabelleTrophyWiiU.png", "demo-message-4", "ChatMessage"})
|
||||||
messages = append(messages, &Message{"Judy", "myohmy, I'm dancing my dreams away.", "https://vignette.wikia.nocookie.net/animalcrossing/images/5/50/NH-Judy_poster.png/revision/latest/scale-to-width-down/100?cb=20200522063219", "demo-message-5"})
|
messages = append(messages, &ChatMessage{"Judy", "myohmy, I'm dancing my dreams away.", "https://vignette.wikia.nocookie.net/animalcrossing/images/5/50/NH-Judy_poster.png/revision/latest/scale-to-width-down/100?cb=20200522063219", "demo-message-5", "ChatMessage"})
|
||||||
messages = append(messages, &Message{"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"})
|
messages = append(messages, &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"})
|
||||||
|
|
||||||
return &Server{
|
server := &Server{
|
||||||
pattern,
|
pattern,
|
||||||
messages,
|
messages,
|
||||||
clients,
|
clients,
|
||||||
addCh,
|
addCh,
|
||||||
delCh,
|
delCh,
|
||||||
sendAllCh,
|
sendAllCh,
|
||||||
|
pingCh,
|
||||||
doneCh,
|
doneCh,
|
||||||
errCh,
|
errCh,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
server.ping()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ClientCount() int {
|
func (s *Server) ClientCount() int {
|
||||||
|
@ -61,7 +78,7 @@ func (s *Server) Del(c *Client) {
|
||||||
s.delCh <- c
|
s.delCh <- c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) SendAll(msg *Message) {
|
func (s *Server) SendAll(msg *ChatMessage) {
|
||||||
s.sendAllCh <- msg
|
s.sendAllCh <- msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,12 +96,21 @@ func (s *Server) sendPastMessages(c *Client) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) sendAll(msg *Message) {
|
func (s *Server) sendAll(msg *ChatMessage) {
|
||||||
for _, c := range s.clients {
|
for _, c := range s.clients {
|
||||||
c.Write(msg)
|
c.Write(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) ping() {
|
||||||
|
// fmt.Println("Start pinging....", len(s.clients))
|
||||||
|
|
||||||
|
ping := &PingMessage{"PING"}
|
||||||
|
for _, c := range s.clients {
|
||||||
|
c.pingch <- ping
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Listen and serve.
|
// Listen and serve.
|
||||||
// It serves client connection and broadcast request.
|
// It serves client connection and broadcast request.
|
||||||
func (s *Server) Listen() {
|
func (s *Server) Listen() {
|
||||||
|
@ -123,6 +149,9 @@ func (s *Server) Listen() {
|
||||||
s.messages = append(s.messages, msg)
|
s.messages = append(s.messages, msg)
|
||||||
s.sendAll(msg)
|
s.sendAll(msg)
|
||||||
|
|
||||||
|
case ping := <-s.pingCh:
|
||||||
|
fmt.Println("PING?", ping)
|
||||||
|
|
||||||
case err := <-s.errCh:
|
case err := <-s.errCh:
|
||||||
log.Println("Error:", err.Error())
|
log.Println("Error:", err.Error())
|
||||||
|
|
||||||
|
@ -130,4 +159,5 @@ func (s *Server) Listen() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
8
utils.go
8
utils.go
|
@ -74,18 +74,18 @@ func resetDirectories(configuration Config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientIDFromRequest(req *http.Request) string {
|
func getClientIDFromRequest(req *http.Request) string {
|
||||||
var ipAddress string
|
var clientID string
|
||||||
xForwardedFor := req.Header.Get("X-FORWARDED-FOR")
|
xForwardedFor := req.Header.Get("X-FORWARDED-FOR")
|
||||||
if xForwardedFor != "" {
|
if xForwardedFor != "" {
|
||||||
ipAddress = xForwardedFor
|
clientID = xForwardedFor
|
||||||
} else {
|
} else {
|
||||||
ipAddressString := req.RemoteAddr
|
ipAddressString := req.RemoteAddr
|
||||||
ipAddressComponents := strings.Split(ipAddressString, ":")
|
ipAddressComponents := strings.Split(ipAddressString, ":")
|
||||||
ipAddressComponents[len(ipAddressComponents)-1] = ""
|
ipAddressComponents[len(ipAddressComponents)-1] = ""
|
||||||
ipAddress = strings.Join(ipAddressComponents, ":")
|
clientID = strings.Join(ipAddressComponents, ":")
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmt.Println("IP address determined to be", ipAddress)
|
// fmt.Println("IP address determined to be", ipAddress)
|
||||||
|
|
||||||
return ipAddress
|
return clientID + req.UserAgent()
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ function setupApp() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getStatus() {
|
async function getStatus() {
|
||||||
let url = "https://util.real-ity.com:8042/status";
|
let url = "https://goth.land/status";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
|
@ -57,11 +57,18 @@ var websocketReconnectTimer;
|
||||||
function setupWebsocket() {
|
function setupWebsocket() {
|
||||||
clearTimeout(websocketReconnectTimer)
|
clearTimeout(websocketReconnectTimer)
|
||||||
|
|
||||||
const protocol = location.protocol == "https:" ? "wss" : "ws"
|
// Uncomment to point to somewhere other than goth.land
|
||||||
var ws = new WebSocket("wss://util.real-ity.com:8042/entry")
|
// const protocol = location.protocol == "https:" ? "wss" : "ws"
|
||||||
|
// var ws = new WebSocket(protocol + "://" + location.host + "/entry")
|
||||||
|
|
||||||
|
var ws = new WebSocket("wss://goth.land/entry")
|
||||||
|
|
||||||
ws.onmessage = (e) => {
|
ws.onmessage = (e) => {
|
||||||
const model = JSON.parse(e.data)
|
const model = JSON.parse(e.data)
|
||||||
|
|
||||||
|
// Ignore non-chat messages (such as keepalive PINGs)
|
||||||
|
if (model.type !== SocketMessageTypes.CHAT) { return; }
|
||||||
|
|
||||||
const message = new Message(model)
|
const message = new Message(model)
|
||||||
|
|
||||||
const existing = this.messagesContainer.messages.filter(function (item) {
|
const existing = this.messagesContainer.messages.filter(function (item) {
|
||||||
|
|
|
@ -11,8 +11,6 @@ class Config {
|
||||||
const response = await fetch(configFileLocation);
|
const response = await fetch(configFileLocation);
|
||||||
const configData = await response.json();
|
const configData = await response.json();
|
||||||
Object.assign(this, configData);
|
Object.assign(this, configData);
|
||||||
console.log(this);
|
|
||||||
|
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
// No config file present. That's ok. It's not required.
|
// No config file present. That's ok. It's not required.
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
|
const SocketMessageTypes = {
|
||||||
|
CHAT: "CHAT",
|
||||||
|
PING: "PING"
|
||||||
|
}
|
||||||
|
|
||||||
class Message {
|
class Message {
|
||||||
constructor(model) {
|
constructor(model) {
|
||||||
this.author = model.author;
|
this.author = model.author;
|
||||||
this.body = model.body;
|
this.body = model.body;
|
||||||
this.image = model.image || "https://robohash.org/" + model.author;
|
this.image = model.image || "https://robohash.org/" + model.author;
|
||||||
this.id = model.id;
|
this.id = model.id;
|
||||||
|
this.type = model.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
addNewlines(str) {
|
addNewlines(str) {
|
||||||
|
|
Loading…
Reference in a new issue