Example #1
0
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 * 2)
			// 	fmt.Println("kick")
			// 	service.Notify("gate", cham.PTYPE_GO, KICK, sessionid)
			// }()
		}
		return cham.NORET
	}
}
Example #2
0
//service self
func Start(service *cham.Service, args ...interface{}) cham.Dispatch {
	log.Infoln("New Service ", service.String())
	mul := new(Multicast)
	mul.channel = 0
	mul.groups = make(map[uint32]map[cham.Address]cham.NULL, DEFAULT_GROUP_SIZE)

	return func(session int32, source cham.Address, ptype uint8, args ...interface{}) []interface{} {
		cmd := args[0].(uint8)
		channel := args[1].(uint32)
		addr := args[2].(cham.Address)

		result := cham.NORET

		switch cmd {
		case NEW:
			result = cham.Ret(mul.new(addr))
		case SUB:
			mul.sub(addr, channel)
		case PUB:
			mul.pub(addr, channel, args[3:]...)
		case UNSUB:
			mul.unsub(addr, channel)
		case DEL:
			mul.del(channel)
		}
		return result
	}
}
Example #3
0
//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
}
Example #4
0
func brokerStart(service *cham.Service, args ...interface{}) cham.Dispatch {
	log.Infoln("New Service ", service.String())
	um := args[0].(*usermanager.UserManager)
	return func(session int32, source cham.Address, ptype uint8, args ...interface{}) []interface{} {
		cmd := args[0].(uint8)
		switch cmd {
		case user.DELETE_USER:
			openid := args[1].(string)
			um.Delete(openid)
		}
		return cham.NORET
	}
}
Example #5
0
File: gate.go Project: hefju/cham
func Start(service *cham.Service, args ...interface{}) cham.Dispatch {
	log.Infoln("New Service ", service.String())
	gate := New(0, service)
	return func(session int32, source cham.Address, ptype uint8, args ...interface{}) []interface{} {
		cmd := args[0].(uint8)
		result := cham.NORET
		switch cmd {
		case OPEN:
			gate.Source = source
			service.RegisterProtocol(cham.PTYPE_RESPONSE, ResponseStart, gate)
			gate.open(args[1].(*Conf))
		case KICK:
			gate.kick(args[1].(uint32))
		}

		return result
	}
}
Example #6
0
func Start(service *cham.Service, args ...interface{}) cham.Dispatch {
	log.Infoln("New Service ", service.String())
	openid, session := args[0].(string), args[1].(uint32)
	user, err := newUser(service, &model.UserModel{Openid: openid}, session)
	if err != nil {
		log.Errorln("Service ", service.String(), "init error,", err.Error())
		service.Stop()
	}
	return func(session int32, source cham.Address, ptype uint8, args ...interface{}) []interface{} {
		cmd := args[0].(uint8)
		switch cmd {
		case CHANGE_SESSION:
			user.session = args[1].(uint32)
		}

		return cham.NORET
	}
}