|
|
@ -11,44 +11,57 @@ type message struct {
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
|
|
|
|
Success bool `json:"success"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func newError(content string) message {
|
|
|
|
|
|
|
|
return message{
|
|
|
|
|
|
|
|
Sender: "",
|
|
|
|
|
|
|
|
Content: content,
|
|
|
|
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
|
|
|
|
Success: false,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func newMessage(sender string, content string) message {
|
|
|
|
|
|
|
|
return message{
|
|
|
|
|
|
|
|
Sender: sender,
|
|
|
|
|
|
|
|
Content: content,
|
|
|
|
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
|
|
|
|
Success: true,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m message) dispatch() {
|
|
|
|
func (m message) dispatch() {
|
|
|
|
for client := range connections {
|
|
|
|
for client := range usernames {
|
|
|
|
client.WriteJSON(m)
|
|
|
|
_ = client.WriteJSON(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var connections = make(map[*websocket.Conn]string)
|
|
|
|
var usernames = make(map[*websocket.Conn]string)
|
|
|
|
|
|
|
|
|
|
|
|
func sendChatMessage(sender *websocket.Conn, msg string) {
|
|
|
|
func sendChatMessage(sender *websocket.Conn, msg string) {
|
|
|
|
m := message{
|
|
|
|
m := newMessage(usernames[sender], msg)
|
|
|
|
Sender: connections[sender],
|
|
|
|
|
|
|
|
Content: msg,
|
|
|
|
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m.dispatch()
|
|
|
|
m.dispatch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func handleDisconnection(sender *websocket.Conn) {
|
|
|
|
func handleDisconnection(sender *websocket.Conn) {
|
|
|
|
m := message{
|
|
|
|
m := newMessage("server", usernames[sender]+" has left the chat.")
|
|
|
|
Sender: "server",
|
|
|
|
|
|
|
|
Content: connections[sender] + " has left the chat",
|
|
|
|
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m.dispatch()
|
|
|
|
m.dispatch()
|
|
|
|
delete(connections, sender)
|
|
|
|
delete(usernames, sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func handleIncomingMessage(sender *websocket.Conn, msg string) {
|
|
|
|
func handleIncomingMessage(sender *websocket.Conn, msg string) {
|
|
|
|
if _, ok := connections[sender]; !ok {
|
|
|
|
if _, ok := usernames[sender]; !ok {
|
|
|
|
connections[sender] = strings.TrimSpace(msg)
|
|
|
|
username := strings.TrimSpace(msg)
|
|
|
|
|
|
|
|
if username == "" || username == "server" {
|
|
|
|
m := message{
|
|
|
|
sender.WriteJSON(newError("You have an illegal nickname"))
|
|
|
|
Sender: "server",
|
|
|
|
return
|
|
|
|
Content: msg + " has joined the chat",
|
|
|
|
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
usernames[sender] = username
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m := newMessage("server", username+" has joined the chat")
|
|
|
|
m.dispatch()
|
|
|
|
m.dispatch()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|