-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (94 loc) · 2.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
gubrak "github.com/novalagung/gubrak/v2"
)
type M map[string]interface{}
const MESSAGE_NEW_USER = "New User"
const MESSAGE_CHAT = "Chat"
const MESSAGE_LEAVE = "Leave"
var connections = make([]*WebSocketConnection, 0)
type SocketPayload struct {
Message string
}
type SocketResponse struct {
From string
Type string
Message string
}
type WebSocketConnection struct {
*websocket.Conn
Username string
}
func main() {
cwd, _ := os.Getwd()
i := 0
router := gin.Default()
upgrader := websocket.Upgrader{
WriteBufferSize: 1024,
ReadBufferSize: 1024,
}
router.LoadHTMLFiles(fmt.Sprintf("%s/%s", cwd, "index.html"))
router.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.html", nil)
})
router.GET("/ws", func(ctx *gin.Context) {
currentGorillaConn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
if err != nil {
http.Error(ctx.Writer, "Could not open websocket connection", http.StatusBadRequest)
}
username := ctx.Query("username")
i++
currentConn := &WebSocketConnection{Conn: currentGorillaConn, Username: username}
connections = append(connections, currentConn)
go handleIO(currentConn, connections)
})
fmt.Println("Server starting at :10020")
router.Run(":10020")
}
func handleIO(currentConn *WebSocketConnection, connections []*WebSocketConnection) {
defer func() {
if r := recover(); r != nil {
log.Println("ERROR", fmt.Sprintf("%v", r))
}
}()
broadcastMessage(currentConn, MESSAGE_NEW_USER, "")
for {
payload := SocketPayload{}
err := currentConn.ReadJSON(&payload)
if err != nil {
if strings.Contains(err.Error(), "websocket: close") {
broadcastMessage(currentConn, MESSAGE_LEAVE, "")
ejectConnection(currentConn)
return
}
log.Println("ERROR", err.Error())
continue
}
broadcastMessage(currentConn, MESSAGE_CHAT, payload.Message)
}
}
func ejectConnection(currentConn *WebSocketConnection) {
filtered := gubrak.From(connections).Reject(func(each *WebSocketConnection) bool {
return each == currentConn
}).Result()
connections = filtered.([]*WebSocketConnection)
}
func broadcastMessage(currentConn *WebSocketConnection, kind, message string) {
for _, eachConn := range connections {
if eachConn == currentConn {
continue
}
eachConn.WriteJSON(SocketResponse{
From: currentConn.Username,
Type: kind,
Message: message,
})
}
}