func clientDispatch(service *cham.Service, args ...interface{}) cham.Dispatch { return func(session int32, source cham.Address, ptype uint8, args ...interface{}) []interface{} { sessionid := args[0].(uint32) gt := args[1].(uint8) time.Sleep(time.Second * 5) switch gt { case OnOpen: fmt.Println("OnOpen ", sessionid) case OnClose: fmt.Println("OnClose ", sessionid, args[2:]) case OnPong: fmt.Println("OnPong ", sessionid, args[2]) case OnMessage: data := string(args[2].([]byte)) fmt.Println("OnMessage", sessionid, data) if data == "hello" { service.Notify("gate", cham.PTYPE_RESPONSE, sessionid, []byte("world"+strconv.Itoa(int(sessionid)))) } else if data == "WebSocket" { service.Notify("gate", cham.PTYPE_RESPONSE, sessionid, []byte("websocket reply"+strconv.Itoa(int(sessionid)))) } go func() { time.Sleep(time.Second * 5) fmt.Println("kick") // service.Notify("gate", cham.PTYPE_GO, KICK, sessionid) err := service.Call("gate", cham.PTYPE_RESPONSE, sessionid, []byte("world")) fmt.Println(err) }() } return cham.NORET } }
//Channel base on a service, args[0] is N worker to dispatch msg func New(service *cham.Service, channel uint32, start cham.Start, args ...interface{}) *Channel { multicast = cham.UniqueService("multicast", Start, args...) if channel == 0 { channel = service.Call(multicast, cham.PTYPE_GO, NEW, uint32(0), service.Addr)[0].(uint32) } service.RegisterProtocol(cham.PTYPE_MULTICAST, start) c := &Channel{service, channel} return c }