棋牌游戏-1

思考并回答以下问题:

framework/net/ws_connection.go

无任何依赖,就是围绕着一个map,把一些数据放进和读取

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
package net

import "sync"

type Session struct {
sync.RWMutex
Cid string // 一个随机的字符串
Uid string // 用户id
data map[string]any
}

func NewSession(cid string) *Session {
return &Session{
Cid: cid,
data: make(map[string]any),
}
}

func (s *Session) Put(key string, v any) {
s.Lock()
defer s.Unlock()
s.data[key] = v
}
func (s *Session) Get(key string) (any, bool) {
s.RLock()
defer s.RUnlock()
v, ok := s.data[key]
return v, ok
}
func (s *Session) SetData(uid string, data map[string]any) {
s.Lock()
defer s.Unlock()
if s.Uid == uid {
for k, v := range data {
s.data[k] = v
}
}
}

framework/net/connection.go

1
2
3
4
5
6
7
8
9
10
11
12
package net

type Connection interface {
Close()
SendMessage(buf []byte) error
GetSession() *Session
}

type MsgPack struct {
Cid string
Body []byte
}
0%