func NewSessionRpcServer() *SessionRpcServer { ctx := context.GetContext() sessionService, ok := ctx.GetComponent("cosession").(*cosession.CoSession) if ok == false || sessionService == nil { sessionService = cosession.NewCoSession() } return &SessionRpcServer{sessionService} }
func main() { ctx := context.GetContext() allOpts := make(map[string]map[string]interface{}) cosessOpts := make(map[string]interface{}) //指定同一个uid可以绑定多个session cosessOpts["multiBind"] = "yes" allOpts["cosession"] = cosessOpts ctx.AllOpts = allOpts defer seelog.Flush() currentServer := make(map[string]interface{}) currentServer["id"] = "connector-1" currentServer["serverType"] = "connector" currentServer["host"] = "127.0.0.1" currentServer["port"] = 8888 ctx.CurrentServer = currentServer cosess := cosession.NewCoSession() cosessRS := cosessionrpcserver.NewCoSessionRpcServer() corpcS := corpcserver.NewCoRpcServer() // 启动cosession,cosessionrpcserver,corpcserver cosess.Start() cosessRS.Start() corpcS.Start() cosess.CreateSession(1, "connector-1", nil) cosess.BindUID("Zhang San", 1) cosess.CreateSession(2, "connector-1", nil) cosess.BindUID("Li Si", 2) cosess.CreateSession(3, "connector-1", nil) cosess.BindUID("Li Si", 3) cosess.CreateSession(4, "connector-1", nil) cosess.BindUID("Wang Wu", 4) ch := make(chan int) <-ch }
/// 创建新的CoConnetor组件. /// /// 创建CoConnector组件时,CoConnector组件使用到的CoSession组件,CoConnection组件一同创建, /// CoServer组件则通过Context拿到,在CoConnector组件启动时加载. func NewCoConnector(ctx *context.Context) *CoConnector { cocnct, ok := ctx.GetComponent("coconnector").(*CoConnector) if ok == true { return cocnct } var decode func([]byte) (interface{}, error) var encode func(string, string, map[string]interface{}) ([]byte, error) cnct := getConnector(ctx) if opts, ok := ctx.AllOpts["coconnector"]; ok == true { decode, _ = opts["decode"].(func([]byte) (interface{}, error)) encode, _ = opts["encode"].(func(string, string, map[string]interface{}) ([]byte, error)) } coserv, ok1 := ctx.GetComponent("coserver").(*coserver.CoServer) if ok1 == false { coserv = coserver.NewCoServer() } cosess, ok2 := ctx.GetComponent("cosession").(*cosession.CoSession) if ok2 == false { cosess = cosession.NewCoSession() } coconn, ok3 := ctx.GetComponent("coconnection").(*coconnection.CoConnection) if ok3 == false { coconn = coconnection.NewCoConnection() } cocnct = &CoConnector{ctx, cnct, coserv, cosess, coconn, decode, encode} ctx.RegisteComponent("coconnector", cocnct) return cocnct }