Commit 4ea4e7c0 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement chat history.

parent 1d90f443
...@@ -127,7 +127,7 @@ func startClient(conn *websocket.Conn) (err error) { ...@@ -127,7 +127,7 @@ func startClient(conn *websocket.Conn) (err error) {
defer close(c.done) defer close(c.done)
c.writeCh = make(chan interface{}, 1) c.writeCh = make(chan interface{}, 25)
defer func() { defer func() {
if isWSNormalError(err) { if isWSNormalError(err) {
err = nil err = nil
...@@ -693,6 +693,20 @@ func clientLoop(c *client, conn *websocket.Conn) error { ...@@ -693,6 +693,20 @@ func clientLoop(c *client, conn *websocket.Conn) error {
cc.action(pushTracksAction{c}) cc.action(pushTracksAction{c})
} }
h := c.group.getChatHistory()
for _, m := range h {
err := c.write(clientMessage{
Type: "chat",
Id: m.id,
Username: m.user,
Value: m.value,
Me: m.me,
})
if err != nil {
return err
}
}
ticker := time.NewTicker(2 * time.Second) ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop() defer ticker.Stop()
...@@ -819,6 +833,7 @@ func handleClientMessage(c *client, m clientMessage) error { ...@@ -819,6 +833,7 @@ func handleClientMessage(c *client, m clientMessage) error {
log.Printf("ICE: %v", err) log.Printf("ICE: %v", err)
} }
case "chat": case "chat":
c.group.addToChatHistory(m.Id, m.Username, m.Value, m.Me)
clients := c.group.getClients(c) clients := c.group.getClients(c)
for _, cc := range clients { for _, cc := range clients {
cc.write(m) cc.write(m)
......
...@@ -51,6 +51,13 @@ type client struct { ...@@ -51,6 +51,13 @@ type client struct {
up map[string]*upConnection up map[string]*upConnection
} }
type chatHistoryEntry struct {
id string
user string
value string
me bool
}
type group struct { type group struct {
name string name string
dead bool dead bool
...@@ -58,6 +65,7 @@ type group struct { ...@@ -58,6 +65,7 @@ type group struct {
mu sync.Mutex mu sync.Mutex
clients []*client clients []*client
history []chatHistoryEntry
} }
type delPCAction struct { type delPCAction struct {
...@@ -259,6 +267,30 @@ func (g *group) Range(f func(c *client) bool) { ...@@ -259,6 +267,30 @@ func (g *group) Range(f func(c *client) bool) {
} }
} }
const maxChatHistory = 20
func (g *group) addToChatHistory(id, user, value string, me bool) {
g.mu.Lock()
defer g.mu.Unlock()
if len(g.history) >= maxChatHistory {
copy(g.history, g.history[1:])
g.history = g.history[:len(g.history)-1]
}
g.history = append(g.history,
chatHistoryEntry{id: id, user: user, value: value, me: me},
)
}
func (g *group) getChatHistory() []chatHistoryEntry {
g.mu.Lock()
g.mu.Unlock()
h := make([]chatHistoryEntry, len(g.history))
copy(h, g.history)
return h
}
type writerDeadError int type writerDeadError int
func (err writerDeadError) Error() string { func (err writerDeadError) Error() string {
......
...@@ -283,6 +283,7 @@ function serverConnect() { ...@@ -283,6 +283,7 @@ function serverConnect() {
}; };
socket.onopen = function(e) { socket.onopen = function(e) {
resetUsers(); resetUsers();
resetChat();
setConnected(true); setConnected(true);
let up = getUserPass(); let up = getUserPass();
send({ send({
...@@ -648,6 +649,11 @@ function addToChatbox(peerId, nick, message, me){ ...@@ -648,6 +649,11 @@ function addToChatbox(peerId, nick, message, me){
return message; return message;
} }
function resetChat() {
lastMessage = {};
document.getElementById('box').textContent = '';
}
function handleInput() { function handleInput() {
let username = getUsername(); let username = getUsername();
let input = document.getElementById('input'); let input = document.getElementById('input');
...@@ -729,6 +735,7 @@ function handleInput() { ...@@ -729,6 +735,7 @@ function handleInput() {
addToChatbox(myid, username, message, me); addToChatbox(myid, username, message, me);
send({ send({
type: 'chat', type: 'chat',
id: myid,
username: username, username: username,
value: message, value: message,
me: me, me: me,
......
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