项目作者: jjware

项目描述 :
A minimalist Stomp client library
高级语言: Go
项目地址: git://github.com/jjware/stomp.git
创建时间: 2019-09-25T21:48:23Z
项目社区:https://github.com/jjware/stomp

开源协议:MIT License

下载


Connection Examples

Using the Frame API

The Frame API provides you with everything you need to get started communicating with a stomp
server.

  1. package main
  2. import (
  3. "errors"
  4. "github.com/jjware/stomp"
  5. "log"
  6. "net"
  7. "strings"
  8. )
  9. func main() {
  10. conn, dialErr := net.Dial("tcp", "http://somesite.com:6464")
  11. if dialErr != nil {
  12. log.Fatal(dialErr)
  13. }
  14. frmConnect := stomp.NewFrame(stomp.CmdConnect, nil)
  15. frmConnect.Header.Set(stomp.HdrLogin, "username")
  16. frmConnect.Header.Set(stomp.HdrPasscode, "password")
  17. frmConnect.Header.Set(stomp.HdrAcceptVersion, "2.0")
  18. writeErr := frmConnect.Write(conn)
  19. if writeErr != nil {
  20. log.Fatal(writeErr)
  21. }
  22. frmResponse, readErr := stomp.ReadFrame(conn)
  23. if readErr != nil {
  24. log.Fatal(readErr)
  25. }
  26. if frmResponse.Command == stomp.CmdError {
  27. if message, ok := frmResponse.Header[stomp.HdrMessage]; ok {
  28. log.Fatal(errors.New(strings.Join(message, ":")))
  29. }
  30. log.Fatal("unknown error")
  31. }
  32. if frmResponse.Command != stomp.CmdConnected {
  33. log.Fatalf("unexpected frame command: %s", frmResponse.Command)
  34. }
  35. // connection established
  36. }

The stomp connection establishment flow is synchronous, so the code hasn’t become
complicated yet. However, directly writing to and reading from a connection through the Frame
API is not thread safe. Invoking Frame::Write concurrently on the same connection may result in
malformed frames on the wire. Invoking stomp.ReadFrame concurrently on the same connection may
result in errors. Even more specifically, invoking stomp.ReadFrame in succession on the same
connection before invoking Close the body of the previously read frame may result in errors.

Using a Handle

A Handle provides thread safe methods for writing frames to and reading frames from a connection.
A context with a timeout may be provided to a handle’s methods. A Handle can be obtained by
calling the stomp.Bind function and passing in the desired connection.

  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/jjware/stomp"
  6. "log"
  7. "net"
  8. "strings"
  9. "time"
  10. )
  11. func main() {
  12. conn, dialErr := net.Dial("tcp", "http://someuri.com:61614")
  13. if dialErr != nil {
  14. log.Fatal(dialErr)
  15. }
  16. handle := stomp.Bind(conn)
  17. frmConnect := stomp.NewFrame(stomp.CmdConnect, nil)
  18. frmConnect.Header.Set(stomp.HdrLogin, "username")
  19. frmConnect.Header.Set(stomp.HdrPasscode, "password")
  20. frmConnect.Header.Set(stomp.HdrAcceptVersion, "2.0")
  21. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  22. sendErr := handle.Send(ctx, frmConnect)
  23. if sendErr != nil {
  24. log.Fatal(sendErr)
  25. }
  26. frmResponse, readErr := handle.Receive(ctx)
  27. cancel()
  28. if readErr != nil {
  29. log.Fatal(readErr)
  30. }
  31. if frmResponse.Command == stomp.CmdError {
  32. if message, ok := frmResponse.Header[stomp.HdrMessage]; ok {
  33. log.Fatal(errors.New(strings.Join(message, ":")))
  34. }
  35. log.Fatal("unknown error")
  36. }
  37. if frmResponse.Command != stomp.CmdConnected {
  38. log.Fatalf("unexpected frame command: %s", frmResponse.Command)
  39. }
  40. // connection established
  41. }