|
|
@ -7,8 +7,44 @@ import (
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var usernames = make(map[*websocket.Conn]string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleIncomingMessage(sender *websocket.Conn, msg string) {
|
|
|
|
|
|
|
|
if _, ok := usernames[sender]; !ok {
|
|
|
|
|
|
|
|
username := strings.TrimSpace(msg)
|
|
|
|
|
|
|
|
if username == "" || username == "server" {
|
|
|
|
|
|
|
|
sender.WriteJSON(newError("You have an illegal nickname"))
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sendUserList(sender)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
usernames[sender] = username
|
|
|
|
|
|
|
|
m := newMessage(msgJoin, "server", username)
|
|
|
|
|
|
|
|
m.dispatch()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sendChatMessage(sender, msg)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleDisconnection(sender *websocket.Conn) {
|
|
|
|
|
|
|
|
m := newMessage(msgLeave, "server", usernames[sender])
|
|
|
|
|
|
|
|
m.dispatch()
|
|
|
|
|
|
|
|
delete(usernames, sender)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type messageType string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
msgChat messageType = "message"
|
|
|
|
|
|
|
|
msgJoin messageType = "join"
|
|
|
|
|
|
|
|
msgLeave messageType = "leave"
|
|
|
|
|
|
|
|
msgErr messageType = "error"
|
|
|
|
|
|
|
|
msgUserList messageType = "users"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type message struct {
|
|
|
|
type message struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Type messageType `json:"type"`
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Content interface{} `json:"content"`
|
|
|
|
Content interface{} `json:"content"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
@ -17,7 +53,7 @@ type message struct {
|
|
|
|
|
|
|
|
|
|
|
|
func newError(content string) message {
|
|
|
|
func newError(content string) message {
|
|
|
|
return message{
|
|
|
|
return message{
|
|
|
|
Type: "error",
|
|
|
|
Type: msgErr,
|
|
|
|
Sender: "",
|
|
|
|
Sender: "",
|
|
|
|
Content: content,
|
|
|
|
Content: content,
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
Date: time.Now().UTC(),
|
|
|
@ -25,7 +61,7 @@ func newError(content string) message {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newMessage(msgType string, sender string, content string) message {
|
|
|
|
func newMessage(msgType messageType, sender string, content string) message {
|
|
|
|
return message{
|
|
|
|
return message{
|
|
|
|
Type: msgType,
|
|
|
|
Type: msgType,
|
|
|
|
Sender: sender,
|
|
|
|
Sender: sender,
|
|
|
@ -41,8 +77,6 @@ func (m message) dispatch() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var usernames = make(map[*websocket.Conn]string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func sendUserList(who *websocket.Conn) {
|
|
|
|
func sendUserList(who *websocket.Conn) {
|
|
|
|
list := []string{}
|
|
|
|
list := []string{}
|
|
|
|
for _, username := range usernames {
|
|
|
|
for _, username := range usernames {
|
|
|
@ -50,7 +84,7 @@ func sendUserList(who *websocket.Conn) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m := message{
|
|
|
|
m := message{
|
|
|
|
Type: "users",
|
|
|
|
Type: msgUserList,
|
|
|
|
Sender: "",
|
|
|
|
Sender: "",
|
|
|
|
Content: list,
|
|
|
|
Content: list,
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
Date: time.Now().UTC(),
|
|
|
@ -61,30 +95,6 @@ func sendUserList(who *websocket.Conn) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func sendChatMessage(sender *websocket.Conn, msg string) {
|
|
|
|
func sendChatMessage(sender *websocket.Conn, msg string) {
|
|
|
|
m := newMessage("message", usernames[sender], msg)
|
|
|
|
m := newMessage(msgChat, usernames[sender], msg)
|
|
|
|
m.dispatch()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleDisconnection(sender *websocket.Conn) {
|
|
|
|
|
|
|
|
m := newMessage("leave", "server", usernames[sender])
|
|
|
|
|
|
|
|
m.dispatch()
|
|
|
|
m.dispatch()
|
|
|
|
delete(usernames, sender)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handleIncomingMessage(sender *websocket.Conn, msg string) {
|
|
|
|
|
|
|
|
if _, ok := usernames[sender]; !ok {
|
|
|
|
|
|
|
|
username := strings.TrimSpace(msg)
|
|
|
|
|
|
|
|
if username == "" || username == "server" {
|
|
|
|
|
|
|
|
sender.WriteJSON(newError("You have an illegal nickname"))
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sendUserList(sender)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
usernames[sender] = username
|
|
|
|
|
|
|
|
m := newMessage("join", "server", username)
|
|
|
|
|
|
|
|
m.dispatch()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sendChatMessage(sender, msg)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|