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

SetToken in SetResponse #251

Closed
Tracked by #368
arigon opened this issue Sep 25, 2021 · 9 comments
Closed
Tracked by #368

SetToken in SetResponse #251

arigon opened this issue Sep 25, 2021 · 9 comments

Comments

@arigon
Copy link

arigon commented Sep 25, 2021

Hi

I'm using SetResponse for responses to confirmable requests. Is it somehow possible to set the Token in SetResponse?
SetResponse is using the pool.Message which has SetToken available. So it should be easily possible to add SetToken into SetResponse but it would be a breaking change.

Alex

@jkralik
Copy link
Member

jkralik commented Sep 28, 2021

Hi. The token is driven by go-coap. But what you can do is that you don't send a response via SetResponse but via WriteMessage where you can put your token. Something similar as is in

err := sendResponse(w.Client(), r.Token, subded, -1)

@arigon
Copy link
Author

arigon commented Sep 28, 2021

I tried, but WriteMessage has no ACK.

@jkralik
Copy link
Member

jkralik commented Sep 28, 2021

ACK is sent via in separate message. So you just send a CON message.

@arigon
Copy link
Author

arigon commented Sep 28, 2021

Can you please give an example?

@jkralik
Copy link
Member

jkralik commented Sep 28, 2021

eg:

func sendResponse(cc mux.Client, token []byte) error {
       m := message.Message{
		Code:    codes.Content,
		Token:   token,
		Context: cc.Context(),
		Body:    bytes.NewReader([]byte("data")),
	}
	var opts message.Options
	var buf []byte
	opts, n, err := opts.SetContentFormat(buf, message.TextPlain)
	if err == message.ErrTooSmall {
		buf = append(buf, make([]byte, n)...)
		opts, n, err = opts.SetContentFormat(buf, message.TextPlain)
	}
	if err != nil {
		return fmt.Errorf("cannot set content format to response: %w", err)
	}
	m.Options = opts
	return cc.WriteMessage(&m)
}


mux.HandlerFunc(func(w mux.ResponseWriter, r *mux.Message) {
  // we send response in another goroutine - so the peer gets separate message
  go func() {
    time.Sleep(time.Second*10) // do something
    err := sendResponse(w.Client(), r.Token)
    if err != nil {
      log.Printf("Error on transmitter: %v", err)
    }
  }()
})

@arigon
Copy link
Author

arigon commented Sep 28, 2021

But where is the ACK?

@jkralik
Copy link
Member

jkralik commented Sep 28, 2021 via email

@arigon
Copy link
Author

arigon commented Sep 28, 2021

Well it doesn't. I tried it these days (I used the WriteMessage from the example) and the client (a sensor) isn't receiving the ACK. It only works with SetResponse.

@jkralik jkralik mentioned this issue Jul 30, 2022
12 tasks
@jkralik
Copy link
Member

jkralik commented Oct 15, 2022

In v3 you can edit response message as you need it.

func (r *ResponseWriter[C]) Message() *pool.Message {

Feel free to reopen the issue.

@jkralik jkralik closed this as completed Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants