本文整理匯總了Golang中FishChatServer/libnet.Session類的典型用法代碼示例。如果您正苦於以下問題:Golang Session類的具體用法?Golang Session怎麽用?Golang Session使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Session類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: procQuitTopic
/*
client -> MsgServer
REQ_QUIT_TOPIC_CMD
arg0: TopicName //群組名
MsgServer -> client
RSP_QUIT_TOPIC_CMD
arg0: SUCCESS/ERROR
*/
func (self *ProtoProc) procQuitTopic(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procQuitTopic")
var err error
if len(cmd.GetArgs()) != 1 {
err = common.SYNTAX_ERROR
} else {
topicName := cmd.GetArgs()[0]
clientID := session.State.(*base.SessionState).ClientID
err = self.msgServer.procQuitTopic(clientID, topicName)
}
resp := protocol.NewCmdSimple(protocol.RSP_QUIT_TOPIC_CMD)
if err != nil {
resp.AddArg(err.Error())
} else {
resp.AddArg(protocol.RSP_SUCCESS)
}
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
return err
}
示例2: procJoinTopic
/*
client -> MsgServer
REQ_JOIN_TOPIC_CMD
arg0: TopicName //群組名
arg1: ClientName //用戶在Topic中的Name, 比如老爸/老媽
MsgServer -> client
RSP_JOIN_TOPIC_CMD
arg0: SUCCESS/FAILED
*/
func (self *ProtoProc) procJoinTopic(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procJoinTopic")
var err error
if len(cmd.GetArgs()) != 2 {
err = common.SYNTAX_ERROR
} else {
topicName := cmd.GetArgs()[0]
clientName := cmd.GetArgs()[1]
clientID := session.State.(*base.SessionState).ClientID
clientType := session.State.(*base.SessionState).ClientType
member := mongo_store.NewMember(clientID, clientName, clientType)
err = self.msgServer.procJoinTopic(member, topicName)
}
resp := protocol.NewCmdSimple(protocol.RSP_JOIN_TOPIC_CMD)
if err != nil {
resp.AddArg(err.Error())
} else {
resp.AddArg(protocol.RSP_SUCCESS)
}
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
return err
}
示例3: procGetTopicList
func (self *ProtoProc) procGetTopicList(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procGetTopicList")
var err error
resp := protocol.NewCmdSimple(protocol.RSP_GET_TOPIC_LIST_CMD)
clientID := session.State.(*base.SessionState).ClientID
//clientType := session.State.(*base.SessionState).ClientType
sessionCacheData, err := self.msgServer.sessionCache.Get(clientID)
if sessionCacheData == nil {
log.Warningf("Client %s not online", clientID)
err = common.NOT_ONLINE
goto ErrOut
}
ErrOut:
if err != nil {
resp.AddArg(err.Error())
} else {
resp.AddArg(protocol.RSP_SUCCESS)
resp.AddArg(strconv.Itoa(len(sessionCacheData.TopicList)))
for _, topic := range sessionCacheData.TopicList {
resp.AddArg(topic)
}
}
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
return err
}
示例4: handleMsgServerClient
func (self *Router) handleMsgServerClient(msc *libnet.Session) {
msc.Process(func(msg *libnet.InBuffer) error {
log.Info("msg_server", msc.Conn().RemoteAddr().String(), " say: ", string(msg.Data))
var c protocol.CmdInternal
pp := NewProtoProc(self)
err := json.Unmarshal(msg.Data, &c)
if err != nil {
log.Error("error:", err)
return err
}
switch c.GetCmdName() {
case protocol.REQ_SEND_P2P_MSG_CMD:
err := pp.procSendMsgP2P(&c, msc)
if err != nil {
log.Warning(err.Error())
}
case protocol.IND_ACK_P2P_STATUS_CMD:
err := pp.procAckP2pStatus(&c, msc)
if err != nil {
log.Warning(err.Error())
}
case protocol.REQ_SEND_TOPIC_MSG_CMD:
err := pp.procSendMsgTopic(&c, msc)
if err != nil {
log.Warning(err.Error())
}
}
return nil
})
}
示例5: handleSession
func handleSession(gw *Gateway, session *libnet.Session) {
session.Process(func(msg *libnet.InBuffer) error {
err := gw.parseProtocol(msg.Data, session)
if err != nil {
log.Error(err.Error())
}
return nil
})
}
示例6: handleSession
func handleSession(ms *MsgServer, session *libnet.Session) {
session.Process(func(msg *libnet.InBuffer) error {
err := ms.parseProtocol(msg.Data, session)
if err != nil {
log.Error(err.Error())
}
return nil
})
}
示例7: procAdd2Topic
/*
client -> MsgServer
REQ_ADD_2_TOPIC_CMD
arg0: TopicName //群組名
arg1: NewClientID //用戶ID
arg2: NewClientName //用戶在Topic中的Name, 對於device, 可以是兒子/女兒
MsgServer -> client
RSP_ADD_2_TOPIC_CMD
arg0: SUCCESS/FAILED
*/
func (self *ProtoProc) procAdd2Topic(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procAdd2Topic")
var err error
topicName := cmd.GetArgs()[0]
mID := cmd.GetArgs()[1]
mName := cmd.GetArgs()[2]
ClientID := session.State.(*base.SessionState).ClientID
ClientType := session.State.(*base.SessionState).ClientType
resp := protocol.NewCmdSimple(protocol.RSP_ADD_2_TOPIC_CMD)
if len(cmd.GetArgs()) != 3 {
err = common.SYNTAX_ERROR
} else
// only DEV_TYPE_CLIENT CAN create topic
if ClientType != protocol.DEV_TYPE_CLIENT {
err = common.DENY_ACCESS
} else {
// check whether the topic exist
topicCacheData, _ := self.msgServer.topicCache.Get(topicName)
if topicCacheData == nil {
log.Infof("TOPIC %s not exist in CACHE", topicName)
err = common.TOPIC_NOT_EXIST
} else
// only topic creater can do this
if topicCacheData.CreaterID != ClientID {
log.Warningf("ClientID %s is not creater of topic %s", ClientID, topicName)
err = common.DENY_ACCESS
} else {
// New Member MUST be online
sessionCacheData, _ := self.msgServer.sessionCache.Get(mID)
if sessionCacheData == nil {
log.Warningf("Client %s not online", mID)
err = common.NOT_ONLINE
} else {
member := mongo_store.NewMember(mID, mName, sessionCacheData.ClientType)
err = self.msgServer.procJoinTopic(member, topicName)
}
}
}
if err != nil {
resp.AddArg(err.Error())
} else {
resp.AddArg(protocol.RSP_SUCCESS)
}
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
return err
}
示例8: procKickTopic
/*
client -> MsgServer
REQ_KICK_TOPIC_CMD
arg0: TopicName //群組名
arg1: NewClientID //待移除的成員用戶ID
MsgServer -> client
RSP_KICK_TOPIC_CMD
arg0: SUCCESS/FAILED
*/
func (self *ProtoProc) procKickTopic(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procKickTopic")
var err error
var topicCacheData *redis_store.TopicCacheData
topicName := cmd.GetArgs()[0]
mID := cmd.GetArgs()[1]
ClientID := session.State.(*base.SessionState).ClientID
ClientType := session.State.(*base.SessionState).ClientType
resp := protocol.NewCmdSimple(protocol.RSP_KICK_TOPIC_CMD)
if len(cmd.GetArgs()) != 2 {
err = common.SYNTAX_ERROR
goto ErrOut
}
// only DEV_TYPE_CLIENT CAN do this
if ClientType != protocol.DEV_TYPE_CLIENT {
err = common.DENY_ACCESS
goto ErrOut
}
// check whether the topic exist
topicCacheData, err = self.msgServer.topicCache.Get(topicName)
if topicCacheData == nil {
log.Warningf("TOPIC %s not exist", topicName)
err = common.TOPIC_NOT_EXIST
} else
// only topic creater can do this
if topicCacheData.CreaterID != ClientID {
log.Warningf("ClientID %s is not creater of topic %s", ClientID, topicName)
err = common.DENY_ACCESS
} else {
err = self.msgServer.procQuitTopic(mID, topicName)
}
ErrOut:
if err != nil {
resp.AddArg(err.Error())
} else {
resp.AddArg(protocol.RSP_SUCCESS)
}
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
return err
}
示例9: procLogin
func (self *ProtoProc) procLogin(cmd protocol.Cmd, session *libnet.Session) error {
//log.Info("procLogin")
var err error
var uuid string
var msgServer string
ClientID := cmd.GetArgs()[0]
ClientType := cmd.GetArgs()[1]
ClientPwd := cmd.GetArgs()[2]
// get the session cache
sessionCacheData, err := self.gateway.sessionCache.Get(ClientID)
if sessionCacheData != nil {
log.Warningf("ID %s already login", ClientID)
msgServer = sessionCacheData.MsgServerAddr
uuid = sessionCacheData.ID
} else {
// choose msg server and allocate UUID
msgServer = self.procGetMinLoadMsgServer()
uuid = common.NewV4().String()
// get the session store to check whether registered
sessionStoreData, _ := self.gateway.mongoStore.GetSessionFromCid(ClientID)
if sessionStoreData == nil {
log.Warningf("ID %s not registered", ClientID)
// for store data
sessionStoreData = mongo_store.NewSessionStoreData(ClientID, ClientPwd, ClientType)
log.Info(sessionStoreData)
common.StoreData(self.gateway.mongoStore, sessionStoreData)
}
// for cache data, MsgServer MUST update local & remote addr.
sessionCacheData = redis_store.NewSessionCacheData(sessionStoreData, session.Conn().RemoteAddr().String(), msgServer, uuid)
log.Info(sessionCacheData)
common.StoreData(self.gateway.sessionCache, sessionCacheData)
}
//
resp := protocol.NewCmdSimple(protocol.RSP_LOGIN_CMD)
resp.AddArg(protocol.RSP_SUCCESS)
resp.AddArg(uuid)
resp.AddArg(msgServer)
log.Info("Resp | ", resp)
if session != nil {
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
session.Close()
log.Info("client ", session.Conn().RemoteAddr().String(), " | close")
}
return nil
}
示例10: procGetTopicMember
func (self *ProtoProc) procGetTopicMember(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procGetTopicMember")
var err error
var topicCacheData *redis_store.TopicCacheData
resp := protocol.NewCmdSimple(protocol.RSP_GET_TOPIC_MEMBER_CMD)
if len(cmd.GetArgs()) != 1 {
err = common.SYNTAX_ERROR
} else {
topicName := cmd.GetArgs()[0]
clientID := session.State.(*base.SessionState).ClientID
//clientType := session.State.(*base.SessionState).ClientType
// check whether the topic exist
topicCacheData, err = self.msgServer.topicCache.Get(topicName)
if topicCacheData == nil {
log.Warningf("TOPIC %s not exist", topicName)
err = common.TOPIC_NOT_EXIST
} else if topicCacheData.MemberExist(clientID) == false {
log.Warningf("%s not the member of topic %d", clientID, topicName)
err = common.DENY_ACCESS
}
}
if err != nil {
resp.AddArg(err.Error())
} else {
resp.AddArg(protocol.RSP_SUCCESS)
resp.AddArg(strconv.Itoa(len(topicCacheData.MemberList)))
for _, member := range topicCacheData.MemberList {
resp.AddArg(member.ID)
resp.AddArg(member.Name)
}
}
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
return err
}
示例11: procLogout
func (self *ProtoProc) procLogout(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procLogout")
var err error
ClientID := session.State.(*base.SessionState).ClientID
resp := protocol.NewCmdSimple(protocol.RSP_LOGOUT_CMD)
resp.AddArg(protocol.RSP_SUCCESS)
err = session.Send(libnet.Json(resp))
if err != nil {
log.Error(err.Error())
}
self.msgServer.procOffline(ClientID)
self.msgServer.sessionCache.Delete(ClientID)
return err
}
示例12: procSendMsgP2P
/*
MsgServer -> Router
REQ_SEND_P2P_MSG_CMD
arg0: Sent2ID //接收方用戶ID
arg1: Msg //消息內容
arg2: FromID //發送方用戶ID
arg3: uuid //MsgServer分配的消息uuid
*/
func (self *ProtoProc) procSendMsgP2P(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procSendMsgP2P")
var err error
send2ID := cmd.GetArgs()[0]
send2Msg := cmd.GetArgs()[1]
log.Info(send2Msg)
self.Router.readMutex.Lock()
defer self.Router.readMutex.Unlock()
cacheSession, err := self.Router.sessionCache.Get(send2ID)
if cacheSession == nil || cacheSession.Alive == false {
storeSession, err := self.Router.mongoStore.GetSessionFromCid(send2ID)
if err != nil {
log.Warningf("ID %s not registered, msg dropped", send2ID)
return err
}
log.Info(storeSession)
log.Warningf("ID registered but offline: %s", send2ID)
cmd.ChangeCmdName(protocol.ROUTE_SEND_P2P_MSG_CMD)
//ms := self.Router.cfg.MsgServerList[0]
ms := session.Conn().RemoteAddr().String()
err = self.Router.msgServerClientMap[ms].Send(libnet.Json(cmd))
if err != nil {
log.Error("error:", err)
return err
}
} else {
log.Info(cacheSession.MsgServerAddr)
cmd.ChangeCmdName(protocol.ROUTE_SEND_P2P_MSG_CMD)
log.Info(cmd)
err = self.Router.msgServerClientMap[cacheSession.MsgServerAddr].Send(libnet.Json(cmd))
if err != nil {
log.Error("error:", err)
return err
}
}
return nil
}
示例13: procSendMsgTopic
/*
Router -> MsgServer
ROUTE_SEND_TOPIC_MSG_CMD
arg0: ClientID //發送方用戶ID
arg1: ClientType //發送方終端類型,是client還是device
arg2: Msg //消息內容
arg3: TopicName //群組名, device無須提供
*/
func (self *ProtoProc) procSendMsgTopic(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procSendMsgTopic")
var err error
ms := session.Conn().RemoteAddr().String()
//send2Msg := cmd.GetArgs()[0]
topicName := cmd.GetArgs()[1]
//fromID := cmd.GetArgs()[2]
//fromType := cmd.GetArgs()[3]
// check whether the topic exist
topicCacheData, err := self.Router.topicCache.Get(topicName)
if topicCacheData == nil {
log.Warningf("TOPIC %s not exist: %s", topicName, err.Error())
return err
}
cmd.ChangeCmdName(protocol.ROUTE_SEND_TOPIC_MSG_CMD)
log.Info(protocol.ROUTE_SEND_TOPIC_MSG_CMD)
log.Info(cmd)
for ip, num := range topicCacheData.AliveMemberNumMap {
if num > 0 {
log.Warningf("topic %s has %d member(s) in ip %s", topicName, num, ip)
if ip != ms {
// not in this server, routing it
err = self.Router.msgServerClientMap[ip].Send(libnet.Json(cmd))
if err != nil {
log.Error("error:", err)
return err
}
}
}
}
return nil
}
示例14: parseProtocol
func (self *MsgServer) parseProtocol(cmd []byte, session *libnet.Session) error {
var c protocol.CmdSimple
err := json.Unmarshal(cmd, &c)
if err != nil {
log.Error("error:", err)
return err
}
pp := NewProtoProc(self)
self.readMutex.Lock()
defer self.readMutex.Unlock()
log.Infof("[%s]->[%s]", session.Conn().RemoteAddr().String(), self.cfg.LocalIP)
log.Info(c)
switch c.GetCmdName() {
case protocol.SEND_PING_CMD:
err = pp.procPing(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.SUBSCRIBE_CHANNEL_CMD:
pp.procSubscribeChannel(&c, session)
case protocol.REQ_LOGIN_CMD:
err = pp.procLogin(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.REQ_LOGOUT_CMD:
err = pp.procLogout(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.REQ_SEND_P2P_MSG_CMD:
err = pp.procSendMessageP2P(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.ROUTE_SEND_P2P_MSG_CMD:
err = pp.procRouteMessageP2P(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
// p2p ack
case protocol.IND_ACK_P2P_STATUS_CMD:
err = pp.procP2pAck(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
// p2p ack
case protocol.ROUTE_ACK_P2P_STATUS_CMD:
err = pp.procP2pAck(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.REQ_SEND_TOPIC_MSG_CMD:
err = pp.procSendTopicMsg(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.ROUTE_SEND_TOPIC_MSG_CMD:
err = pp.procRouteTopicMsg(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.REQ_CREATE_TOPIC_CMD:
err = pp.procCreateTopic(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.REQ_ADD_2_TOPIC_CMD:
err = pp.procAdd2Topic(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
case protocol.REQ_KICK_TOPIC_CMD:
err = pp.procKickTopic(&c, session)
if err != nil {
log.Error("error:", err)
return err
}
//.........這裏部分代碼省略.........
示例15: procSendTopicMsg
/*
device/client -> MsgServer -> Router
REQ_SEND_TOPIC_MSG_CMD
arg0: Msg //消息內容
arg1: TopicName //群組名, device無須提供
返回給消息發送者的消息
MsgServer -> device/client
RSP_SEND_TOPIC_MSG_CMD
arg0: SUCCESS/FAILED
通過Router轉發消息(對終端開發者不可見)
Router -> MsgServer
ROUTE_SEND_TOPIC_MSG_CMD
arg0: Msg //消息內容
arg1: TopicName //群組名
arg2: ClientID //發送方用戶ID
arg3: ClientType //發送方終端類型,是client還是device
發送給消息接受者的消息
MsgServer -> device/client
REQ_SEND_TOPIC_MSG_CMD
arg0: Msg //消息內容
arg1: TopicName //群組名
arg2: ClientID //發送方用戶ID
arg3: ClientType //發送方終端類型,是client還是device
*/
func (self *ProtoProc) procSendTopicMsg(cmd protocol.Cmd, session *libnet.Session) error {
log.Info("procSendTopicMsg")
var err error
var topicName string
var topicCacheData *redis_store.TopicCacheData
var sessionCacheData *redis_store.SessionCacheData
//var sessionStoreData *mongo_store.SessionStoreData
msg := cmd.GetArgs()[0]
resp := protocol.NewCmdSimple(protocol.RSP_SEND_TOPIC_MSG_CMD)
ClientID := session.State.(*base.SessionState).ClientID
ClientType := session.State.(*base.SessionState).ClientType
if ClientType == protocol.DEV_TYPE_CLIENT {
if len(cmd.GetArgs()) != 2 {
err = common.SYNTAX_ERROR
goto ErrOut
}
} else if len(cmd.GetArgs()) != 1 {
err = common.SYNTAX_ERROR
goto ErrOut
}
// get session cache
sessionCacheData, err = self.msgServer.sessionCache.Get(ClientID)
if sessionCacheData == nil {
log.Errorf("ID %s cache missing", ClientID)
err = common.NOT_ONLINE
goto ErrOut
} else if ClientType == protocol.DEV_TYPE_WATCH {
topicName = sessionCacheData.GetTopics()[0]
} else {
topicName = cmd.GetArgs()[1]
}
// check whether the topic exist
topicCacheData, err = self.msgServer.topicCache.Get(topicName)
if topicCacheData == nil {
log.Warningf("TOPIC %s not exist", topicName)
err = common.TOPIC_NOT_EXIST
} else {
topic_msg_resp := protocol.NewCmdSimple(cmd.GetCmdName())
topic_msg_resp.AddArg(msg)
topic_msg_resp.AddArg(topicName)
topic_msg_resp.AddArg(ClientID)
topic_msg_resp.AddArg(ClientType)
if topicCacheData.AliveMemberNumMap[self.msgServer.cfg.LocalIP] > 0 {
// exactly in this server, just broadcasting
log.Warningf("topic %s has %d member(s) in this server", topicName, topicCacheData.AliveMemberNumMap[self.msgServer.cfg.LocalIP])
for _, mID := range topicCacheData.MemberList {
if self.msgServer.sessions[mID.ID] != nil {
self.msgServer.sessions[mID.ID].Send(libnet.Json(topic_msg_resp))
if err != nil {
log.Fatalln(err.Error())
}
}
}
}
if self.msgServer.channels[protocol.SYSCTRL_SEND] != nil {
//topic_msg_resp.ChangeCmdName(protocol.ROUTE_SEND_TOPIC_MSG_CMD)
for ip, num := range topicCacheData.AliveMemberNumMap {
if num > 0 {
log.Warningf("topic %s has %d member(s) in ip %s", topicName, num, ip)
if ip != self.msgServer.cfg.LocalIP {
// not in this server, routing it
_, err = self.msgServer.channels[protocol.SYSCTRL_SEND].Channel.Broadcast(libnet.Json(topic_msg_resp))
if err != nil {
log.Error(err.Error())
}
break
}
//.........這裏部分代碼省略.........