Exemplo n.º 1
0
func client(cmd *cobra.Command, args []string) {
	// Instantiates a new Client
	c = &service.Client{}

	// Creates a new MQTT CONNECT message and sets the proper parameters
	msg := message.NewConnectMessage()
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte(fmt.Sprintf("pingmqclient%d%d", os.Getpid(), time.Now().Unix())))
	msg.SetKeepAlive(300)

	// Connects to the remote server at 127.0.0.1 port 1883
	if err := c.Connect(clientURI, msg); err != nil {
		log.Fatal(err)
	}

	// Creates a new SUBSCRIBE message to subscribe to topic "abc"
	submsg := message.NewSubscribeMessage()

	for _, t := range clientTopics {
		submsg.AddTopic([]byte(t), 0)
	}

	c.Subscribe(submsg, nil, onPublish)

	<-done
}
Exemplo n.º 2
0
func NewSurgeMQ(numberOfMessages int, testLatency bool) *SurgeMQ {
	uri := "tcp://127.0.0.1:1883"

	msg := message.NewConnectMessage()
	msg.SetWillQos(1)
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte("surgemq"))
	msg.SetKeepAlive(10)
	msg.SetWillTopic([]byte("will"))
	msg.SetWillMessage([]byte("send me home"))
	msg.SetUsername([]byte("surgemq"))
	msg.SetPassword([]byte("verysecret"))

	c, err := service.Connect(service.Context{
		KeepAlive:      service.DefaultKeepAlive,
		ConnectTimeout: service.DefaultConnectTimeout,
		AckTimeout:     service.DefaultAckTimeout,
		TimeoutRetries: service.DefaultTimeoutRetries,
		Auth:           auth.MockSuccessAuthenticator,
		Topics:         topics.NewMemTopics(),
		Store:          sessions.NewMemStore(),
	}, uri, msg)

	if err != nil {
		panic(err)
	}

	var handler benchmark.MessageHandler
	if testLatency {
		handler = &benchmark.LatencyMessageHandler{
			NumberOfMessages: numberOfMessages,
			Latencies:        []float32{},
		}
	} else {
		handler = &benchmark.ThroughputMessageHandler{NumberOfMessages: numberOfMessages}
	}

	return &SurgeMQ{
		handler:     handler,
		subject:     "test",
		client:      c,
		testLatency: testLatency,
	}
}