2019-09-23 12:18:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
type message struct {
|
2019-09-24 03:11:45 +07:00
|
|
|
Type string `json:"type"`
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Content interface{} `json:"content"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
Success bool `json:"success"`
|
2019-09-23 23:23:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newError(content string) message {
|
|
|
|
return message{
|
2019-09-24 03:11:45 +07:00
|
|
|
Type: "error",
|
2019-09-23 23:23:21 +07:00
|
|
|
Sender: "",
|
|
|
|
Content: content,
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
Success: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMessage(sender string, content string) message {
|
|
|
|
return message{
|
2019-09-24 03:11:45 +07:00
|
|
|
Type: "message",
|
2019-09-23 23:23:21 +07:00
|
|
|
Sender: sender,
|
|
|
|
Content: content,
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
Success: true,
|
|
|
|
}
|
2019-09-23 12:18:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m message) dispatch() {
|
2019-09-23 23:23:21 +07:00
|
|
|
for client := range usernames {
|
|
|
|
_ = client.WriteJSON(m)
|
2019-09-23 12:18:00 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 23:23:21 +07:00
|
|
|
var usernames = make(map[*websocket.Conn]string)
|
2019-09-23 12:18:00 +07:00
|
|
|
|
2019-09-24 03:11:45 +07:00
|
|
|
func sendUserList(who *websocket.Conn) {
|
|
|
|
list := []string{}
|
|
|
|
for _, username := range usernames {
|
|
|
|
list = append(list, username)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := message{
|
|
|
|
Type: "users",
|
|
|
|
Sender: "",
|
|
|
|
Content: list,
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
Success: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = who.WriteJSON(m)
|
|
|
|
}
|
|
|
|
|
2019-09-23 12:18:00 +07:00
|
|
|
func sendChatMessage(sender *websocket.Conn, msg string) {
|
2019-09-23 23:23:21 +07:00
|
|
|
m := newMessage(usernames[sender], msg)
|
2019-09-23 12:18:00 +07:00
|
|
|
m.dispatch()
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDisconnection(sender *websocket.Conn) {
|
2019-09-23 23:23:21 +07:00
|
|
|
m := newMessage("server", usernames[sender]+" has left the chat.")
|
2019-09-23 12:18:00 +07:00
|
|
|
m.dispatch()
|
2019-09-23 23:23:21 +07:00
|
|
|
delete(usernames, sender)
|
2019-09-23 12:18:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleIncomingMessage(sender *websocket.Conn, msg string) {
|
2019-09-23 23:23:21 +07:00
|
|
|
if _, ok := usernames[sender]; !ok {
|
|
|
|
username := strings.TrimSpace(msg)
|
|
|
|
if username == "" || username == "server" {
|
|
|
|
sender.WriteJSON(newError("You have an illegal nickname"))
|
|
|
|
return
|
2019-09-23 12:18:00 +07:00
|
|
|
}
|
2019-09-25 01:06:11 +07:00
|
|
|
sendUserList(sender)
|
2019-09-23 23:23:21 +07:00
|
|
|
|
|
|
|
usernames[sender] = username
|
|
|
|
m := newMessage("server", username+" has joined the chat")
|
2019-09-23 12:18:00 +07:00
|
|
|
m.dispatch()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendChatMessage(sender, msg)
|
|
|
|
}
|