diff --git a/chatclient.go b/chatclient.go index e5efb70..bc0dce0 100644 --- a/chatclient.go +++ b/chatclient.go @@ -11,10 +11,6 @@ import ( "github.com/zorchenhimer/MovieNight/common" ) -func connSend(s string, c *websocket.Conn) { - c.WriteMessage(websocket.TextMessage, []byte(s)) -} - type Client struct { name string // Display name conn *websocket.Conn @@ -56,12 +52,19 @@ func (cl *Client) NewMsg(data common.ClientData) { switch data.Type { case common.CdUsers: fmt.Printf("[chat|hidden] <%s> get list of users\n", cl.name) - s, err := common.EncodeHiddenMessage(data.Type, chat.GetNames()) - if err != nil { - fmt.Printf("[ERR] could not encode user list, %v", err) - return + + names := chat.GetNames() + idx := -1 + for i := range names { + if names[i] == cl.name { + idx = i + } + } + + err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, append(names[:idx], names[idx+1:]...))) + if err != nil { + fmt.Printf("Error sending chat data: %v\n", err) } - cl.Send(s) case common.CdMessage: msg := html.EscapeString(data.Message) msg = removeDumbSpaces(msg) @@ -81,7 +84,10 @@ func (cl *Client) NewMsg(data common.ClientData) { response := commands.RunCommand(cmd, args, cl) if response != "" { - cl.ServerMessage(response) + err := cl.SendServerMessage(ParseEmotes(msg)) + if err != nil { + fmt.Printf("Error command results %v\n", err) + } return } @@ -103,6 +109,30 @@ func (cl *Client) NewMsg(data common.ClientData) { } } +func (cl *Client) SendChatData(newData common.NewChatDataFunc) error { + cd, err := newData() + if err != nil { + return fmt.Errorf("could not create chatdata of type %d: %v", cd.Type, err) + } + return cl.Send(cd) +} + +func (cl *Client) Send(data common.ChatData) error { + err := cl.conn.WriteJSON(data) + if err != nil { + return fmt.Errorf("could not send message: %v", err) + } + return nil +} + +func (cl *Client) SendServerMessage(s string) error { + err := cl.SendChatData(common.NewChatMessage("", ColorServerMessage, s, common.MsgServer)) + if err != nil { + return fmt.Errorf("could send server message to %s: %s; Message: %s\n", cl.name, err, s) + } + return nil +} + // Make links clickable func formatLinks(input string) string { newMsg := []string{} @@ -121,22 +151,6 @@ func (cl *Client) Exit() { cl.belongsTo.Leave(cl.name, cl.color) } -//Sending message block to the client -func (cl *Client) Send(s string) { - connSend(s, cl.conn) -} - -// Send server message to this client -func (cl *Client) ServerMessage(msg string) { - msg = ParseEmotes(msg) - encoded, err := common.EncodeMessage("", "#ea6260", msg, common.MsgError) - if err != nil { - fmt.Printf("[ERR] could not server message to %s: %s; Message: %s\n", cl.name, err, msg) - return - } - cl.Send(encoded) -} - // Outgoing messages func (cl *Client) Message(msg string) { msg = ParseEmotes(msg) diff --git a/chatcommands.go b/chatcommands.go index 7cfc332..2f28855 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -229,7 +229,7 @@ var commands = &CommandControl{ common.CNReloadEmotes.String(): Command{ HelpText: "Reload the emotes on the server.", Function: func(cl *Client, args []string) string { - cl.ServerMessage("Reloading emotes") + cl.SendServerMessage("Reloading emotes") num, err := LoadEmotes() if err != nil { fmt.Printf("Unbale to reload emotes: %s\n", err) diff --git a/chatroom.go b/chatroom.go index 3351a38..72d2456 100644 --- a/chatroom.go +++ b/chatroom.go @@ -18,8 +18,9 @@ import ( ) const ( - UsernameMaxLength int = 36 - UsernameMinLength int = 3 + UsernameMaxLength int = 36 + UsernameMinLength int = 3 + ColorServerMessage string = "#ea6260" ) var re_username *regexp.Regexp = regexp.MustCompile(`^[0-9a-zA-Z_-]+$`) @@ -29,8 +30,8 @@ type ChatRoom struct { clientsMtx sync.Mutex tempConn map[string]*websocket.Conn - queue chan string - modqueue chan string // mod and admin broadcast messages + queue chan common.ChatData + modqueue chan common.ChatData // mod and admin broadcast messages playing string playingLink string @@ -42,8 +43,8 @@ type ChatRoom struct { //initializing the chatroom func newChatRoom() (*ChatRoom, error) { cr := &ChatRoom{ - queue: make(chan string, 100), - modqueue: make(chan string, 100), + queue: make(chan common.ChatData, 100), + modqueue: make(chan common.ChatData, 100), clients: make(map[string]*Client), tempConn: make(map[string]*websocket.Conn), } @@ -159,7 +160,7 @@ func (cr *ChatRoom) Join(name, uid string) (*Client, error) { delete(cr.tempConn, uid) fmt.Printf("[join] %s %s\n", host, name) - playingCommand, err := common.EncodeCommand(common.CmdPlaying, []string{cr.playing, cr.playingLink}) + playingCommand, err := common.NewChatCommand(common.CmdPlaying, []string{cr.playing, cr.playingLink})() if err != nil { fmt.Printf("Unable to encode playing command on join: %s\n", err) } else { @@ -265,15 +266,9 @@ func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) { t = common.MsgServer } - data, err := common.EncodeMessage( - from.name, - from.color, - msg, - t) - + data, err := common.NewChatMessage(from.name, from.color, msg, t)() if err != nil { fmt.Printf("Error encoding chat message: %s", err) - cr.queue <- msg return } @@ -285,8 +280,7 @@ func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) { } func (cr *ChatRoom) AddCmdMsg(command common.CommandType, args []string) { - data, err := common.EncodeCommand(command, args) - + data, err := common.NewChatCommand(command, args)() if err != nil { fmt.Printf("Error encoding command: %s", err) return @@ -300,7 +294,7 @@ func (cr *ChatRoom) AddCmdMsg(command common.CommandType, args []string) { } func (cr *ChatRoom) AddModNotice(message string) { - data, err := common.EncodeMessage("", "", message, common.MsgNotice) + data, err := common.NewChatMessage("", "", message, common.MsgNotice)() if err != nil { fmt.Printf("Error encoding notice: %v", err) return @@ -314,7 +308,7 @@ func (cr *ChatRoom) AddModNotice(message string) { } func (cr *ChatRoom) AddEventMsg(event common.EventType, name, color string) { - data, err := common.EncodeEvent(event, name, color) + data, err := common.NewChatEvent(event, name, color)() if err != nil { fmt.Printf("Error encoding command: %s", err) @@ -338,7 +332,7 @@ func (cr *ChatRoom) Unmod(name string) error { } client.Unmod() - client.ServerMessage(`You have been unmodded.`) + client.SendServerMessage(`You have been unmodded.`) return nil } @@ -352,7 +346,7 @@ func (cr *ChatRoom) Mod(name string) error { } client.IsMod = true - client.ServerMessage(`You have been modded.`) + client.SendServerMessage(`You have been modded.`) return nil } @@ -378,17 +372,14 @@ func (cr *ChatRoom) UserCount() int { func (cr *ChatRoom) BroadCast() { running := true for running { + cr.clientsMtx.Lock() select { case msg := <-cr.queue: - if len(msg) > 0 { - cr.clientsMtx.Lock() - for _, client := range cr.clients { - client.Send(msg) - } - for _, conn := range cr.tempConn { - connSend(msg, conn) - } - cr.clientsMtx.Unlock() + for _, client := range cr.clients { + client.Send(msg) + } + for _, conn := range cr.tempConn { + conn.WriteJSON(msg) } default: // No messages to send @@ -399,18 +390,15 @@ func (cr *ChatRoom) BroadCast() { // Mod queue select { case msg := <-cr.modqueue: - if len(msg) > 0 { - cr.clientsMtx.Lock() - for _, client := range cr.clients { - if client.IsMod || client.IsAdmin { - client.Send(msg) - } + for _, client := range cr.clients { + if client.IsMod || client.IsAdmin { + client.Send(msg) } - cr.clientsMtx.Unlock() } default: running = false } + cr.clientsMtx.Unlock() } } diff --git a/common/chatdata.go b/common/chatdata.go index 9e46a35..2da9075 100644 --- a/common/chatdata.go +++ b/common/chatdata.go @@ -6,6 +6,8 @@ import ( "fmt" ) +type NewChatDataFunc func() (ChatData, error) + type DataInterface interface { HTML() string } @@ -26,10 +28,6 @@ func (c ChatData) GetData() (DataInterface, error) { d := DataMessage{} err = json.Unmarshal(c.Data, &d) data = d - case DTError: - d := DataError{} - err = json.Unmarshal(c.Data, &d) - data = d case DTCommand: d := DataCommand{} err = json.Unmarshal(c.Data, &d) @@ -71,22 +69,6 @@ func (c ClientData) HTML() string { return `