go-websockets-talk/code/hub.go

71 lines
1.4 KiB
Go

package main
import (
"strings"
"time"
"github.com/gorilla/websocket"
)
type message struct {
Sender string `json:"sender"`
Content string `json:"content"`
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() {
for client := range usernames {
_ = client.WriteJSON(m)
}
}
var usernames = make(map[*websocket.Conn]string)
func sendChatMessage(sender *websocket.Conn, msg string) {
m := newMessage(usernames[sender], msg)
m.dispatch()
}
func handleDisconnection(sender *websocket.Conn) {
m := newMessage("server", usernames[sender]+" has left the chat.")
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
}
usernames[sender] = username
m := newMessage("server", username+" has joined the chat")
m.dispatch()
return
}
sendChatMessage(sender, msg)
}