diff --git a/hasaki-push/api_push_message.go b/hasaki-push/api_push_message.go index 8b82cb4..d576dc7 100644 --- a/hasaki-push/api_push_message.go +++ b/hasaki-push/api_push_message.go @@ -41,19 +41,71 @@ func SetMessageHandler(messageType string, f MessageHandler) { messageHandlerMap.Store(messageType, f) } -// LOGIN -type SessionType string +// 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 ( - Unknown SessionType = "unknown" // 未知 - PadType SessionType = "pad" // 平板 - AdminType SessionType = "admin" // 管理员 - UserType SessionType = "user" // 用户 - CoachType SessionType = "coach" // 教练 + NoDevice DeviceType = "no_device" + WebServer DeviceType = "web" + Android DeviceType = "android" + IosPhone DeviceType = "ios" + WechatMini DeviceType = "wechat_mini" ) type Session struct { *melody.Session - Type SessionType `json:"type"` - UserId int `json:"user_id"` + 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() { } diff --git a/hasaki-push/initialize.go b/hasaki-push/initialize.go index 21002ab..db2cc24 100644 --- a/hasaki-push/initialize.go +++ b/hasaki-push/initialize.go @@ -2,44 +2,45 @@ package hasaki_push import ( "git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin" + "git.ouxuan.net/hasaki-service/hasaki-sdk/hskutils" "git.ouxuan.net/tommy/melody" ) func Initialize(router *hskgin.GinHelper) { - mrouter := melody.New() + node := NewNode() + // PAD_MESSAGE router.Any("/push/message", func(ctx *hskgin.GinContextHelper) { - err := mrouter.HandleRequest(ctx.GinContext.Writer, ctx.GinContext.Request) + err := node.HandleRequest(ctx.GinContext.Writer, ctx.GinContext.Request) ctx.CheckErrDisplayByError(err) }) // 连接 - mrouter.HandleConnect(func(session *melody.Session) { - + node.HandleConnect(func(session *melody.Session) { + node.SetSession("unknown-"+hskutils.CreateUUID(), NewSession(session)) // 连接 }) // 断连 - mrouter.HandleDisconnect(func(session *melody.Session) { - + node.HandleDisconnect(func(session *melody.Session) { }) // 接收信息 - mrouter.HandleMessage(func(session *melody.Session, message []byte) { + node.HandleMessage(func(session *melody.Session, message []byte) { // 根据消息类型进行各种 }) // 发送消息 - mrouter.HandleSentMessage(func(session *melody.Session, bytes []byte) { + node.HandleSentMessage(func(session *melody.Session, bytes []byte) { // 设置发送状态 }) // 错误 - mrouter.HandleError(func(session *melody.Session, err error) { + node.HandleError(func(session *melody.Session, err error) { }) // 关闭 - mrouter.HandleClose(func(session *melody.Session, i int, s string) error { + node.HandleClose(func(session *melody.Session, i int, s string) error { return nil }) }