Commit 88d2a968 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Restructure error and chat messages.

parent 7b512962
......@@ -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},
)
}
......
......@@ -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);
......
......@@ -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:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment