code: refactor variable names and remove unnecessary logs

master
ALI Hamza 2019-09-24 06:23:21 +07:00
parent c2bb0098db
commit 4eacf3195b
No known key found for this signature in database
GPG Key ID: 7C608266BF384ADC
3 changed files with 45 additions and 30 deletions

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

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -18,22 +17,16 @@ var (
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)
if err != nil { if err != nil {
fmt.Fprintf(w, "Internal server error: %s", err) _, _ = fmt.Fprint(w, "You must use the web socket protocol to connect to this endpoint.", err)
fmt.Printf("Internal server error: %s", err)
return return
} }
defer ws.Close() defer ws.Close()
ws.WriteMessage(websocket.TextMessage, []byte("nickname"))
for { for {
_, p, err := ws.ReadMessage() _, p, err := ws.ReadMessage()
if err != nil { if err != nil {
handleDisconnection(ws) handleDisconnection(ws)
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
}
break break
} }

@ -1 +1,10 @@
<p>sawp</p> <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Go Chat Application</title>
</head>
<body>
</body>
</html>