-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
131 lines (108 loc) · 2.76 KB
/
app.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
model "web-service/model"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
var todoList []string
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func getActionAndTask(input string) (string, string) {
inputArr := strings.Split(input, " ")
return inputArr[0], inputArr[1]
}
func updateTodoList(input string) {
tmpList := todoList
todoList = []string{}
for _, val := range tmpList {
if val == input {
continue
}
todoList = append(todoList, val)
}
}
type user struct {
ID string `json:"id"`
Name string `json:"name"`
Age float64 `json:"age"`
}
var users = []user{
{ID: "1", Name: "Shashank Rai", Age: 27},
{ID: "2", Name: "Saurabh Kumar", Age: 28},
{ID: "3", Name: "Prashant Sharma", Age: 27},
}
func getUsers(c *gin.Context) {
c.IndentedJSON(http.StatusOK, users)
}
var activeSocketUsers map[int]*websocket.Conn = make(map[int]*websocket.Conn)
func startSocketMessaging(conn *websocket.Conn) {
// Continuosly read and write message
for {
_, rawMessage, err := conn.ReadMessage()
if err != nil {
log.Println("read failed:", err)
break
}
fmt.Println(string(rawMessage))
body := model.ParseToModel(rawMessage)
fmt.Printf("body: %v\n", body)
fmt.Printf("body: %v\n", body.GetCmd())
if body.GetCmd() == "add" {
todoList = append(todoList, body.GetTodo())
} else if body.GetCmd() == "done" {
updateTodoList(body.GetTodo())
}
messageRedirecting(body.Receiver, body)
output := "Ongoing Tasks: \n"
if len(todoList) == 0 {
output += "Empty"
} else {
for _, todo := range todoList {
output += "\n - " + todo + "\n"
}
}
sendMessage(conn, output)
}
}
func sendMessage(conn *websocket.Conn, message string) {
err := conn.WriteMessage(websocket.TextMessage, []byte(message))
if err != nil {
log.Println("write failed:", err)
// conn.Close()
}
}
func messageRedirecting(receiver int, message model.Message) {
if activeSocketUsers[receiver] != nil {
sendMessage(activeSocketUsers[receiver], string(model.TransformToJson(message)))
}
}
func main() {
router := gin.Default()
router.GET("/users", getUsers)
router.GET("/ws", func(c *gin.Context) {
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return
}
defer conn.Close()
query_params := c.Request.URL.Query()
user_id, err := strconv.Atoi(query_params.Get("user_id"))
if err != nil {
panic("Please set user id as query params in the websocket request")
}
if activeSocketUsers[user_id] != nil {
return
}
activeSocketUsers[user_id] = conn
conn.WriteMessage(websocket.TextMessage, []byte("Hello, WebSocket!. You are now connected"))
startSocketMessaging(conn)
})
router.Run("localhost:8080")
}