func NewBackendSessionService(ctx *context.Context) *BackendSessionService { rpcClient, ok := ctx.GetComponent("corpcclient").(*corpcclient.CoRpcClient) if ok == false || rpcClient == nil { rpcClient = corpcclient.NewCoRpcClient() } return &BackendSessionService{rpcClient} }
/// 创建新的ChannelService,ChannelService管理用户创建的所有Channel,并代理用户对Channel的操作, /// 用户发送给一个Channel里所有session的消息会通过rpc调用发送给对应的前端服务器,并通过前端服务 /// 器推送给客户端. func NewChannelService() *ChannelService { ctx := context.GetContext() coRpcClient, ok := ctx.GetComponent("corpcclient").(*corpcclient.CoRpcClient) if ok == false { coRpcClient = corpcclient.NewCoRpcClient() } channels := make(map[string]*Channel) return &ChannelService{ctx, coRpcClient, channels} }
func NewServer() *Server { ctx := context.GetContext() coRpcS, ok1 := ctx.GetComponent("corpcserver").(*corpcserver.CoRpcServer) if ok1 == false { coRpcS = corpcserver.NewCoRpcServer() } coRpcC, ok2 := ctx.GetComponent("corpcclient").(*corpcclient.CoRpcClient) if ok2 == false { coRpcC = corpcclient.NewCoRpcClient() } handlers := make(map[string]func(*sessionService.SimpleSession, map[string]interface{}) map[string]interface{}) return &Server{ctx, coRpcS, coRpcC, handlers} }
func main() { ctx := context.GetContext() defer seelog.Flush() currentServer := make(map[string]interface{}) currentServer["id"] = "chat-1" currentServer["serverType"] = "chat" currentServer["host"] = "127.0.0.1" currentServer["port"] = 8888 ctx.CurrentServer = currentServer cobackendS := cobackendsession.NewCoBackendSession() corpcC := corpcclient.NewCoRpcClient() cobackendS.Start() corpcC.Start() backendS := cobackendS.GetBackendSessionBySID("connector-1", 1) if backendS != nil { fmt.Printf("--------------GetBackendSessionBySID(%v,%v)--------------\n", "connector-1", 1) fmt.Printf("frontendid:%v sid:%v uid:%v\n", backendS.GetFrontendID(), backendS.GetID(), backendS.GetUID()) fmt.Println("---------------------------------------------------------\n") backendS.UnbindUID(backendS.GetUID()) fmt.Printf("--------------UnBindUID(%v)--------------\n", backendS.GetUID()) backendSN := cobackendS.GetBackendSessionBySID("connector-1", 1) if backendSN != nil { fmt.Printf("--------------After UnBindUID(%v)--------------\n", backendS.GetUID()) fmt.Printf("frontendid:%v sid:%v uid:%v\n", backendSN.GetFrontendID(), backendSN.GetID(), backendSN.GetUID()) fmt.Println("-----------------------------------------------------\n") } backendS.SetOpt("age", 12) fmt.Printf("--------------SetOpt(%v,%v)--------------\n", "age", 12) backendS.PushOpt("age") fmt.Printf("--------------PushOpt(%v)--------------\n", "age") } backendS = cobackendS.GetBackendSessionBySID("connector-1", 1) if backendS != nil { fmt.Printf("--------------GetBackendSessionBySID(%v,%v)--------------\n", "connector-1", backendS.GetID()) fmt.Printf("frontendid:%v sid:%v uid:%v,opts:%v\n", backendS.GetFrontendID(), backendS.GetID(), backendS.GetUID(), backendS.GetOpts()) fmt.Println("--------------------------------------------------------------\n") } infoByUID := cobackendS.GetBackendSessionsByUID("connector-1", "Li Si") if infoByUID != nil { fmt.Printf("--------------GetBackendSessionSByUID(%v,%v)--------------\n", "connector-1", "Li Si") for _, elem := range infoByUID { fmt.Printf("frontend id:%v,id:%v,uid:%v \n", elem.GetFrontendID(), elem.GetID(), elem.GetUID()) } fmt.Println("---------------------------------------------------\n") } cobackendS.KickBySID("connector-1", 1, "no reason") fmt.Printf("--------------KickBySID(%v,%v,%v)--------------\n", "connector-1", 1, "no reason") backendK := cobackendS.GetBackendSessionBySID("connector-1", 1) if backendK == nil { fmt.Printf("--------------After Kick sid<%v>,session not exists--------------\n\n", 1) } }