func TestGateWsService(t *testing.T) { gs := cham.NewService("gate", Start, 16) // 16 worker to send data to clie ws := cham.NewService("watchdog", watchDogStart, 16) // 16 worker to process client data ws.RegisterProtocol(cham.PTYPE_CLIENT, clientDispatch) ws.Call(gs, cham.PTYPE_GO, OPEN, NewConf("127.0.0.1:9998", 100, "/ws")) time.Sleep(time.Minute * 10) }
func main() { gs := cham.NewService("gate", gate.Start, 8) um := usermanager.New() bs := cham.NewService("broker", brokerStart, 8, um) bs.RegisterProtocol(cham.PTYPE_CLIENT, brokerDispatch, um) bs.Call(gs, cham.PTYPE_GO, gate.OPEN, gate.NewConf("127.0.0.1:9998", 0, "/ws")) cham.NewService("debug", debug.Start, 1, "127.0.0.1:8888") // lobby := cham.NewService("lobby", lobby.Start) cham.Run() }
func TestGateService(t *testing.T) { ws := cham.NewService("watchdog", watchDogStart, 16) // 16 worker to process client data ws.RegisterProtocol(cham.PTYPE_CLIENT, clientDispatch) gs := cham.NewService("gate", Start, 16) // 16 worker to send data to client ws.Call(gs, cham.PTYPE_GO, OPEN, NewConf("127.0.0.1:9999", 100, "")) for i := 0; i < 20; i++ { go runClient(i) } time.Sleep(time.Minute * 2) }
func TestMulticast(t *testing.T) { main := cham.NewService("Leader", mainStart) chat1 := cham.NewService("chat1", chatStart) chat2 := cham.NewService("chat2", chatStart) //create a channel channel := New(main, 0, channelStart) //bind a channel chat1Channel := New(chat1, channel.Channel, channelStart) chat2Channel := New(chat2, channel.Channel, channelStart) chat1Channel.Subscribe() chat2Channel.Subscribe() channel.Publish("hello world") chat1Channel.Publish("i am chat1") chat2Channel.Unsubscribe() chat2.Stop() // test stop services channel.Publish("last") channel.Delete() }
func (um *UserManager) Add(request *protocol.Login, session uint32) { openid := request.Openid um.RLock() if us, ok := um.users[openid]; ok { um.RUnlock() us.NotifySelf(cham.PTYPE_GO, user.CHANGE_SESSION, session) return } um.RUnlock() log.Infoln("new user,openid:", openid) us := cham.NewService(fmt.Sprintf("user-%s", openid), user.Start, 1, openid, session) if us == nil { log.Errorln("new user failed, openid:", openid) return } um.Lock() um.users[openid] = us um.Unlock() }
func TestDebug(t *testing.T) { ll := log.New("log.txt", log.LDEFAULT, log.LDEBUG) ll.Infoln("hello") cham.NewService("debug", Start, 1, "127.0.0.1:8888") time.Sleep(time.Minute * 30) }
LINFO LERROR ) const ( LTIME = 1 << iota LFILE LLEVEL LDEFAULT = LTIME | LLEVEL LALL = LTIME | LFILE | LLEVEL ) const TimeFormat = "2006/01/02 15:04:05" var Names = [3]string{"Debug", "Info", "Error"} var logs = cham.NewService("log", Start) type Logger struct { level int flag int out string } //if out empty, use os.Stdout func New(out string, flag int, level int) *Logger { cham.Main.Call(logs, cham.PTYPE_GO, OPEN, out) return &Logger{level, flag, out} } var std = New("", LDEFAULT, LINFO)
func (lobby *Lobby) Allocate() { roomId := lobby.roomManager.NextRoom() roomName := fmt.Sprintf("room-%d", int(roomId)) lobby.rooms[roomId] = cham.NewService(roomName, room.Start, 1, roomId) }