Esempio n. 1
0
func main() {
	c := &service.Client{}

	msgConn := message.NewConnectMessage()
	msgConn.SetVersion(3)
	msgConn.SetCleanSession(true)
	msgConn.SetClientId([]byte("01234567"))
	msgConn.SetKeepAlive(300)

	if err := c.Connect("tcp://127.0.0.1:1883", msgConn); err != nil {
		log.Fatal(err)
	}
	log.Println("connected")

	msgSub := message.NewSubscribeMessage()
	msgSub.AddTopic([]byte("#"), 0)
	onPub := func(msg *message.PublishMessage) error {
		return pub(c, msg)
	}
	onComp := func(msg, ack message.Message, err error) error {
		log.Printf("on complete subscribe: msg=%#v ack=%#v err=%v\n", msg, ack, err)
		return err
	}
	if err := c.Subscribe(msgSub, onComp, onPub); err != nil {
		log.Fatal(err)
	}
	log.Println("subscribed")

	//c.Disconnect()
	//log.Println("disconnected")

	done := make(chan bool)
	<-done
}
Esempio n. 2
0
func newSubscribeMessage(topic string, qos byte) *message.SubscribeMessage {
	msg := message.NewSubscribeMessage()
	msg.SetPacketId(1)
	msg.AddTopic([]byte(topic), qos)

	return msg
}
Esempio n. 3
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
}
Esempio n. 4
0
func (smq *SurgeMQ) Setup() {
	msg := message.NewSubscribeMessage()
	msg.SetPacketId(1)
	msg.AddTopic([]byte(smq.subject), 0)

	smq.client.Subscribe(msg, nil, func(msg *message.PublishMessage) error {
		smq.handler.ReceiveMessage(msg.Payload())
		return nil
	})
}
Esempio n. 5
0
func main() {

	flag.Parse()

	c := service.Client{}

	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"))

	// Connects to the remote server at 127.0.0.1 port 1883
	str := *addr + ":" + *port
	c.Connect("tcp://"+str, msg)

	// Creates a new SUBSCRIBE message to subscribe to topic "abc"
	submsg := message.NewSubscribeMessage()
	submsg.AddTopic([]byte("abc"), 0)

	// Subscribes to the topic by sending the message. The first nil in the function
	// call is a OnCompleteFunc that should handle the SUBACK message from the server.
	// Nil means we are ignoring the SUBACK messages. The second nil should be a
	// OnPublishFunc that handles any messages send to the client because of this
	// subscription. Nil means we are ignoring any PUBLISH messages for this topic.
	c.Subscribe(submsg, onCompleteFunc, onPublishFunc)

	// Creates a new PUBLISH message with the appropriate contents for publishing
	pubmsg := message.NewPublishMessage()
	pubmsg.SetTopic([]byte("abc"))
	pubmsg.SetPayload(make([]byte, 1024))
	pubmsg.SetQoS(0)

	// Publishes to the server by sending the message
	c.Publish(pubmsg, onCompleteFunc)

	// Disconnects from the server
	jklog.L().Infoln("disconnected.")
	for {
		time.Sleep(5000 * time.Millisecond)
		// c.Publish(pubmsg, onCompleteFunc)
	}

	c.Disconnect()
}
Esempio n. 6
0
func ExampleClient() {
	// Instantiates a new Client
	c := &Client{}

	// Creates a new MQTT CONNECT message and sets the proper parameters
	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"))

	// Connects to the remote server at 127.0.0.1 port 1883
	c.Connect("tcp://127.0.0.1:1883", msg)

	// Creates a new SUBSCRIBE message to subscribe to topic "abc"
	submsg := message.NewSubscribeMessage()
	submsg.AddTopic([]byte("abc"), 0)

	// Subscribes to the topic by sending the message. The first nil in the function
	// call is a OnCompleteFunc that should handle the SUBACK message from the server.
	// Nil means we are ignoring the SUBACK messages. The second nil should be a
	// OnPublishFunc that handles any messages send to the client because of this
	// subscription. Nil means we are ignoring any PUBLISH messages for this topic.
	c.Subscribe(submsg, nil, nil)

	// Creates a new PUBLISH message with the appropriate contents for publishing
	pubmsg := message.NewPublishMessage()
	pubmsg.SetTopic([]byte("abc"))
	pubmsg.SetPayload(make([]byte, 1024))
	pubmsg.SetQoS(0)

	// Publishes to the server by sending the message
	c.Publish(pubmsg, nil)

	// Disconnects from the server
	c.Disconnect()
}
Esempio n. 7
0
func newSubscribeMessage(qos byte) *message.SubscribeMessage {
	msg := message.NewSubscribeMessage()
	msg.AddTopic([]byte("abc"), qos)

	return msg
}
Esempio n. 8
0
func TestServiceWillDelivery(t *testing.T) {
	var wg sync.WaitGroup

	ready1 := make(chan struct{})
	ready2 := make(chan struct{})
	ready3 := make(chan struct{})
	subscribers := 3

	uri := "tcp://127.0.0.1:1883"
	u, err := url.Parse(uri)
	require.NoError(t, err, "Error parsing URL")

	// Start listener
	wg.Add(1)
	go startServiceN(t, u, &wg, ready1, ready2, subscribers)

	<-ready1

	c1 := connectToServer(t, uri)
	require.NotNil(t, c1)
	defer topics.Unregister(c1.svc.sess.ID())

	c2 := connectToServer(t, uri)
	require.NotNil(t, c2)
	defer topics.Unregister(c2.svc.sess.ID())

	c3 := connectToServer(t, uri)
	require.NotNil(t, c3)
	defer topics.Unregister(c3.svc.sess.ID())

	sub := message.NewSubscribeMessage()
	sub.AddTopic([]byte("will"), 1)

	subdone := int64(0)
	willdone := int64(0)

	c2.Subscribe(sub,
		func(msg, ack message.Message, err error) error {
			subs := atomic.AddInt64(&subdone, 1)
			if subs == int64(subscribers-1) {
				c1.Disconnect()
			}

			return nil
		},
		func(msg *message.PublishMessage) error {
			require.Equal(t, message.QosAtLeastOnce, msg.QoS())
			require.Equal(t, []byte("send me home"), msg.Payload())

			will := atomic.AddInt64(&willdone, 1)
			if will == int64(subscribers-1) {
				close(ready3)
			}

			return nil
		})

	c3.Subscribe(sub,
		func(msg, ack message.Message, err error) error {
			subs := atomic.AddInt64(&subdone, 1)
			if subs == int64(subscribers-1) {
				c1.Disconnect()
			}

			return nil
		},
		func(msg *message.PublishMessage) error {
			require.Equal(t, message.QosAtLeastOnce, msg.QoS())
			require.Equal(t, []byte("send me home"), msg.Payload())

			will := atomic.AddInt64(&willdone, 1)
			if will == int64(subscribers-1) {
				close(ready3)
			}

			return nil
		})

	select {
	case <-ready3:

	case <-time.After(time.Millisecond * 100):
		require.FailNow(t, "Test timed out")
	}

	c2.Disconnect()

	close(ready2)

	wg.Wait()
}