diff --git a/group.go b/group.go index cb48f60..50fd4d0 100644 --- a/group.go +++ b/group.go @@ -24,8 +24,8 @@ import ( type chatHistoryEntry struct { id string user string + kind string value string - me bool } const ( @@ -294,7 +294,7 @@ func (g *group) clearChatHistory() { g.history = nil } -func (g *group) addToChatHistory(id, user, value string, me bool) { +func (g *group) addToChatHistory(id, user, kind, value string) { g.mu.Lock() defer g.mu.Unlock() @@ -303,7 +303,7 @@ func (g *group) addToChatHistory(id, user, value string, me bool) { g.history = g.history[:len(g.history)-1] } g.history = append(g.history, - chatHistoryEntry{id: id, user: user, value: value, me: me}, + chatHistoryEntry{id: id, user: user, kind: kind, value: value}, ) } diff --git a/static/sfu.js b/static/sfu.js index 3e00ac1..5cec8f0 100644 --- a/static/sfu.js +++ b/static/sfu.js @@ -681,7 +681,7 @@ function serverConnect() { gotUser(m.id, m.username, m.del); break; case 'chat': - addToChatbox(m.id, m.username, m.value, m.me); + addToChatbox(m.id, m.username, m.kind, m.value); break; case 'clearchat': resetChat(); @@ -694,8 +694,15 @@ function serverConnect() { case 'pong': /* nothing */ break; - case 'error': - displayError('The server said: ' + m.value); + case 'usermessage': + switch(m.kind) { + case 'error': + displayError('The server said: ' + m.value); + break; + default: + displayWarning('The server said: ' + m.value) + break; + } break; default: console.warn('Unexpected server message', m.type); @@ -962,10 +969,10 @@ function formatLines(lines) { let lastMessage = {}; -function addToChatbox(peerId, nick, message, me){ +function addToChatbox(peerId, nick, kind, message){ let container = document.createElement('div'); container.classList.add('message'); - if(!me) { + if(kind !== 'me') { let p = formatLines(message.split('\n')); if (lastMessage.nick !== nick || lastMessage.peerId !== peerId) { let user = document.createElement('p'); @@ -1124,8 +1131,8 @@ function handleInput() { type: 'chat', id: myid, username: username, + kind: me ? 'me' : '', value: message, - me: me, }); } catch(e) { console.error(e); diff --git a/webclient.go b/webclient.go index 355972c..81d5f8c 100644 --- a/webclient.go +++ b/webclient.go @@ -175,7 +175,7 @@ type clientMessage struct { Permissions clientPermission `json:"permissions,omitempty"` Group string `json:"group,omitempty"` Value string `json:"value,omitempty"` - Me bool `json:"me,omitempty"` + Kind string `json:"kind,omitempty"` Offer *webrtc.SessionDescription `json:"offer,omitempty"` Answer *webrtc.SessionDescription `json:"answer,omitempty"` Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"` @@ -634,7 +634,8 @@ func startClient(conn *websocket.Conn) (err error) { // at this point, the writer is not running yet, so format // the message ourselves conn.WriteJSON(clientMessage{ - Type: "error", + Type: "usermessage", + Kind: "error", Value: "don't put spaces in your username", }) conn.WriteMessage(websocket.CloseMessage, @@ -667,7 +668,8 @@ func startClient(conn *websocket.Conn) (err error) { m, e := errorToWSCloseMessage(err) if m != "" { c.write(clientMessage{ - Type: "error", + Type: "usermessage", + Kind: "error", Value: m, }) } @@ -727,7 +729,7 @@ func clientLoop(c *webClient, conn *websocket.Conn) error { Id: m.id, Username: m.user, Value: m.value, - Me: m.me, + Kind: m.kind, }) if err != nil { return err @@ -996,7 +998,7 @@ func handleClientMessage(c *webClient, m clientMessage) error { log.Printf("ICE: %v", err) } case "chat": - c.group.addToChatHistory(m.Id, m.Username, m.Value, m.Me) + c.group.addToChatHistory(m.Id, m.Username, m.Kind, m.Value) clients := c.group.getClients(nil) for _, cc := range clients { cc, ok := cc.(*webClient) @@ -1163,7 +1165,8 @@ func (c *webClient) error(err error) error { switch e := err.(type) { case userError: return c.write(clientMessage{ - Type: "error", + Type: "usermessage", + Kind: "error", Value: string(e), }) default: