Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using bytes.Buffer as payload #345

Merged
merged 2 commits into from Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions client.go
Expand Up @@ -18,6 +18,7 @@
package mqtt

import (
"bytes"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -580,11 +581,13 @@ func (c *client) Publish(topic string, qos byte, retained bool, payload interfac
pub.Qos = qos
pub.TopicName = topic
pub.Retain = retained
switch payload.(type) {
switch p := payload.(type) {
case string:
pub.Payload = []byte(payload.(string))
pub.Payload = []byte(p)
case []byte:
pub.Payload = payload.([]byte)
pub.Payload = p
case bytes.Buffer:
pub.Payload = p.Bytes()
default:
token.setError(fmt.Errorf("Unknown payload type"))
return token
Expand Down
18 changes: 18 additions & 0 deletions fvt_client_test.go
Expand Up @@ -142,6 +142,24 @@ func Test_Publish_3(t *testing.T) {
c.Disconnect(250)
}

func Test_Publish_BytesBuffer(t *testing.T) {
ops := NewClientOptions()
ops.AddBroker(FVTTCP)
ops.SetClientID("Publish_BytesBuffer")

c := NewClient(ops)
token := c.Connect()
if token.Wait() && token.Error() != nil {
t.Fatalf("Error on Client.Connect(): %v", token.Error())
}

payload := bytes.NewBufferString("Publish qos0")

c.Publish("test/Publish", 0, false, payload)

c.Disconnect(250)
}

func Test_Subscribe(t *testing.T) {
pops := NewClientOptions()
pops.AddBroker(FVTTCP)
Expand Down