-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (115 loc) · 3.03 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"gopkg.in/olahol/melody.v1"
)
type Message struct {
Event string `json:"event"`
Name string `json:"name"`
Content string `json:"content"`
}
func NewMessage(event, name, content string) *Message {
return &Message{
Event: event,
Name: name,
Content: content,
}
}
func (m *Message) GetByteMessage() []byte {
result, _ := json.Marshal(m)
return result
}
const (
KEY = "chat_id"
WAIT = "wait"
)
var redisClient *redis.Client
var ctx = context.Background()
func InitSession(s *melody.Session) string {
id := uuid.New().String()
s.Set(KEY, id)
return id
}
func GetSessionID(s *melody.Session) string {
if id, isExist := s.Get(KEY); isExist {
return id.(string)
}
return InitSession(s)
}
func GetWaitFirstKey() (string, error) {
return redisClient.LPop(context.Background(), WAIT).Result()
}
func AddToWaitList(id string) error {
fmt.Println("加入等待")
return redisClient.LPush(ctx, WAIT, id).Err()
}
func CreateChat(id1, id2 string) {
redisClient.Set(context.Background(), id1, id2, 0)
redisClient.Set(context.Background(), id2, id1, 0)
}
func RemoveChat(id1, id2 string) {
redisClient.Del(context.Background(), id1, id2)
}
func init() {
redisClient = redis.NewClient(&redis.Options{
Addr: "redis:6379",
DB: 0, // use default DB
})
pong, err := redisClient.Ping(context.Background()).Result()
if err == nil {
log.Println("redis 回應成功,", pong)
} else {
log.Fatal("redis 無法連線,錯誤為", err)
}
}
func main() {
r := gin.Default()
r.LoadHTMLGlob("template/html/*")
r.Static("/assets", "./template/assets")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
m := melody.New()
r.GET("/ws", func(c *gin.Context) {
m.HandleRequest(c.Writer, c.Request)
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
id := GetSessionID(s)
chatTo, _ := redisClient.Get(context.TODO(), id).Result()
m.BroadcastFilter(msg, func(session *melody.Session) bool {
compareID, _ := session.Get(KEY)
return compareID == chatTo || compareID == id
})
})
m.HandleConnect(func(session *melody.Session) {
id := InitSession(session)
if key, err := GetWaitFirstKey(); err == nil && key != "" {
CreateChat(id, key)
msg := NewMessage("other", "對方已經", "加入聊天室").GetByteMessage()
m.BroadcastFilter(msg, func(session *melody.Session) bool {
compareID, _ := session.Get(KEY)
return compareID == id || compareID == key
})
} else {
AddToWaitList(id)
}
})
m.HandleClose(func(session *melody.Session, i int, s string) error {
id := GetSessionID(session)
chatTo, _ := redisClient.Get(context.TODO(), id).Result()
msg := NewMessage("other", "對方已經", "離開聊天室").GetByteMessage()
RemoveChat(id, chatTo)
return m.BroadcastFilter(msg, func(session *melody.Session) bool {
compareID, _ := session.Get(KEY)
return compareID == chatTo
})
})
r.Run(":8888")
}