|
|
@ -25,9 +25,9 @@ func newError(content string) message {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newMessage(sender string, content string) message {
|
|
|
|
func newMessage(msgType string, sender string, content string) message {
|
|
|
|
return message{
|
|
|
|
return message{
|
|
|
|
Type: "message",
|
|
|
|
Type: msgType,
|
|
|
|
Sender: sender,
|
|
|
|
Sender: sender,
|
|
|
|
Content: content,
|
|
|
|
Content: content,
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
Date: time.Now().UTC(),
|
|
|
@ -61,12 +61,12 @@ func sendUserList(who *websocket.Conn) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func sendChatMessage(sender *websocket.Conn, msg string) {
|
|
|
|
func sendChatMessage(sender *websocket.Conn, msg string) {
|
|
|
|
m := newMessage(usernames[sender], msg)
|
|
|
|
m := newMessage("message", usernames[sender], msg)
|
|
|
|
m.dispatch()
|
|
|
|
m.dispatch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func handleDisconnection(sender *websocket.Conn) {
|
|
|
|
func handleDisconnection(sender *websocket.Conn) {
|
|
|
|
m := newMessage("server", usernames[sender]+" has left the chat.")
|
|
|
|
m := newMessage("leave", "server", usernames[sender])
|
|
|
|
m.dispatch()
|
|
|
|
m.dispatch()
|
|
|
|
delete(usernames, sender)
|
|
|
|
delete(usernames, sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -81,7 +81,7 @@ func handleIncomingMessage(sender *websocket.Conn, msg string) {
|
|
|
|
sendUserList(sender)
|
|
|
|
sendUserList(sender)
|
|
|
|
|
|
|
|
|
|
|
|
usernames[sender] = username
|
|
|
|
usernames[sender] = username
|
|
|
|
m := newMessage("server", username+" has joined the chat")
|
|
|
|
m := newMessage("join", "server", username)
|
|
|
|
m.dispatch()
|
|
|
|
m.dispatch()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|