-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
54 lines (41 loc) · 1.03 KB
/
handler.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
package main
import (
"log"
"math/rand"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
// Implement origin checking logic here
return true
},
}
func (client *Client) init() {
intro := yellow(bold("\n----------\n WELCOME TO THE FEED\n HERE YOU TALK IN THE TERMINAL\n ENTER THE PASSWORD AND USERNAME TO START\n----------\n"))
if err := client.Conn.WriteMessage(websocket.TextMessage, []byte(intro)); err != nil {
log.Fatal(" Error connecting: ", err)
}
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
rand.Seed(time.Now().UnixNano())
randomIndex := rand.Intn(len(colors))
randomColor := colors[randomIndex]
client := &Client{
Conn: conn,
msgColor: randomColor,
}
client.init()
client.getUsernamePassword()
client.welcome()
client.handleMessages()
}