You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package ws
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
// Entry 数据对象
|
|
type Entry struct {
|
|
MessageType int // websocket消息类型 RFC 6455, section 11.8.
|
|
Raw []byte // 原始数据
|
|
extData map[string]interface{} // 扩展数据
|
|
mu sync.RWMutex // 读写锁
|
|
}
|
|
|
|
// Set stores kv pair.
|
|
func (e *Entry) Set(key string, value interface{}) {
|
|
e.mu.Lock()
|
|
if e.extData == nil {
|
|
e.extData = make(map[string]interface{})
|
|
}
|
|
e.extData[key] = value
|
|
e.mu.Unlock()
|
|
}
|
|
|
|
// Get retrieves the value according to the key.
|
|
func (e *Entry) Get(key string) (value interface{}, exists bool) {
|
|
e.mu.RLock()
|
|
value, exists = e.extData[key]
|
|
e.mu.RUnlock()
|
|
return
|
|
}
|
|
|
|
// MustGet retrieves the value according to the key.
|
|
// Panics if key does not exist.
|
|
func (e *Entry) MustGet(key string) interface{} {
|
|
if v, ok := e.Get(key); ok {
|
|
return v
|
|
}
|
|
panic(fmt.Errorf("key `%s` does not exist", key))
|
|
}
|
|
|
|
// Remove deletes the key from storage.
|
|
func (e *Entry) Remove(key string) {
|
|
e.mu.Lock()
|
|
delete(e.extData, key)
|
|
e.mu.Unlock()
|
|
}
|