code: refactor code to use enums, and reorder funcs

master
ALI Hamza 2020-08-27 21:14:59 +07:00
parent 1e972b6935
commit 02f22219c6
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
2 changed files with 45 additions and 37 deletions

@ -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)
} }

@ -7,12 +7,10 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var ( var upgrader = websocket.Upgrader{
upgrader = websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
WriteBufferSize: 1024, WriteBufferSize: 1024,
} }
)
func websocketConnection(w http.ResponseWriter, r *http.Request) { func websocketConnection(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)