Ejemplo n.º 1
0
func (h *Handler) HandleMessage(message *nsq.Message) error {
	dataPackage := common.GetDataPackage()
	defer common.PutDataPackage(dataPackage)
	if err := dataPackage.Unpackage(message.Body); err != nil {
		syslog.Warn(err)
		return nil
	}

	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	switch head.Command {
	case common.XXX_CMD_SEND_USER_MSG:
	default:
		syslog.Warn("Invalid Cmd :", head.Command, *tail)
	}
	// get user state
	us, err := user.GetUserState(tail.UID)
	if err != nil {
		syslog.Info("GetUserState failed!!!", tail)
		return nil
	}
	// not online
	if us.Online == false {
		syslog.Debug("client not online!!! uid:", tail.UID)
		return nil
	}

	tail.IP = us.ConnIP
	tail.Port = us.ConnPort
	tail.SID = us.Sid
	tail.UID = us.Uid

	respBuf := common.GetBuffer()
	defer common.PutBuffer(respBuf)
	if err := common.Package(respBuf, head, jsonStr, tail); err != nil {
		syslog.Info(err)
		return nil
	}
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	return nil
}
Ejemplo n.º 2
0
func (h *Handler) HandleMessage(message *nsq.Message) error {
	dataPackage := common.GetDataPackage()
	defer common.PutDataPackage(dataPackage)
	if err := dataPackage.Unpackage(message.Body); err != nil {
		syslog.Warn(err)
		return nil
	}
	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	var err error = nil
	resp := []byte("")
	switch head.Command {
	case common.XXX_CMD_SEND_USER_MSG:
		resp, err = h.SendMsg(head, jsonStr, tail)
	case common.XXX_CMD_RECV_USER_MSG:
		//resp, err = h.ReceivedUserMsg(head, jsonStr, &tail)
		common.NsqPublish(common.MSG_STORE_TOPIC, message.Body)
	default:
		syslog.Info("invalid cmd:", head.Command, *tail)
		return nil
	}

	if err != nil {
		syslog.Warn("msg_server msg handle failed:", err, head, tail)
		head.ErrorCode = uint32(errors.Code(err))
		resp = []byte("")
	}
	head.PkgLen = uint16(common.DATA_PACKAGE_HEAD_SIZE + len(resp))
	respBuf := common.GetBuffer()
	defer common.PutBuffer(respBuf)
	if err := common.Package(respBuf, head, resp, tail); err != nil {
		syslog.Info(err)
		return nil
	}
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	return nil
}