main.go 649 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "sparrow/pkg/coap"
  6. )
  7. const url = "127.0.0.1:56883"
  8. func main() {
  9. co, err := net.Dial("udp", url)
  10. if err != nil {
  11. fmt.Printf("dial err %s", err.Error())
  12. }
  13. token := []byte{
  14. 0x1,
  15. 0x2,
  16. 0x3,
  17. 0x4,
  18. 0x5,
  19. 0x6,
  20. 0x7,
  21. 0x8,
  22. }
  23. req := &coap.BaseMessage{
  24. Code: coap.POST,
  25. Type: coap.CON,
  26. MessageID: 2,
  27. //Token: []byte{0, 1, 0x2, 3, 4, 5, 6, 7, 8, 9, 1, 12, 12, 1, 2, 1, 2, 2},
  28. Token: token,
  29. Payload: []byte("PAYLOAD"),
  30. }
  31. //req.AddOption(coap.URIPath, "/topic/s")
  32. bytes, err := req.Encode()
  33. if err != nil {
  34. fmt.Printf("dial err %s", err.Error())
  35. }
  36. co.Write(bytes)
  37. }