You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.2 KiB
111 lines
2.2 KiB
package hasaki_push
|
|
|
|
import (
|
|
"git.ouxuan.net/tommy/melody"
|
|
"sync"
|
|
)
|
|
|
|
var PushMessageSpanSecond = 5
|
|
|
|
func Send(sender int, receiver int, messageType string, Content interface{}, retries ...int) error {
|
|
retry := 0
|
|
if len(retries) > 0 {
|
|
retry = retries[0]
|
|
}
|
|
return add(&PushMessage{
|
|
Type: messageType,
|
|
Sender: sender,
|
|
Receiver: receiver,
|
|
RetryNumber: retry,
|
|
SpanSecond: PushMessageSpanSecond,
|
|
Content: Content,
|
|
Status: NotSend,
|
|
})
|
|
}
|
|
|
|
func SetStatus(uniqueId string, status MessageStatus) error {
|
|
return setStatus(uniqueId, status)
|
|
}
|
|
|
|
type Message struct {
|
|
UniqueId string `json:"unique_id"`
|
|
Type string `json:"type"`
|
|
Content interface{} `json:"content"`
|
|
}
|
|
|
|
var messageHandlerMap sync.Map
|
|
|
|
type MessageHandler func(session *melody.Session, message []byte)
|
|
|
|
func SetMessageHandler(messageType string, f MessageHandler) {
|
|
messageHandlerMap.Store(messageType, f)
|
|
}
|
|
|
|
// session: 身份 - 某个点
|
|
type Node struct {
|
|
*melody.Melody
|
|
|
|
sessions map[string]map[string]*Session
|
|
mutex sync.Mutex
|
|
}
|
|
|
|
func NewNode() *Node {
|
|
return &Node{
|
|
Melody: melody.New(),
|
|
sessions: map[string]map[string]*Session{},
|
|
mutex: sync.Mutex{},
|
|
}
|
|
}
|
|
|
|
func (n *Node) GetMelody() *melody.Melody {
|
|
return n.Melody
|
|
}
|
|
|
|
func (n *Node) SetSession(uid string, session *Session) {
|
|
n.mutex.Lock()
|
|
if n.sessions == nil {
|
|
n.sessions = map[string]map[string]*Session{}
|
|
}
|
|
//n.sessions[uid] = append(n.sessions[uid], session)
|
|
n.mutex.Unlock()
|
|
}
|
|
|
|
// 身份
|
|
type IdentityType string
|
|
|
|
const (
|
|
Unknown IdentityType = "unknown" // 未知
|
|
Pad IdentityType = "pad" // 平板
|
|
Admin IdentityType = "admin" // 管理员
|
|
User IdentityType = "user" // 用户
|
|
Coach IdentityType = "coach" // 教练
|
|
)
|
|
|
|
type DeviceType string
|
|
|
|
const (
|
|
NoDevice DeviceType = "no_device"
|
|
WebServer DeviceType = "web"
|
|
Android DeviceType = "android"
|
|
IosPhone DeviceType = "ios"
|
|
WechatMini DeviceType = "wechat_mini"
|
|
)
|
|
|
|
type Session struct {
|
|
*melody.Session
|
|
Identity IdentityType
|
|
Device DeviceType
|
|
UserId int
|
|
}
|
|
|
|
func NewSession(session *melody.Session) *Session {
|
|
return &Session{
|
|
Identity: Unknown,
|
|
Device: WechatMini,
|
|
Session: session,
|
|
UserId: 0,
|
|
}
|
|
}
|
|
|
|
func (s *Session) SetUid() {
|
|
}
|