sessions.go 792 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package goSam
  2. import (
  3. "fmt"
  4. "math"
  5. "math/rand"
  6. "time"
  7. )
  8. func init() {
  9. rand.Seed(time.Now().UnixNano())
  10. }
  11. // CreateStreamSession creates a new STREAM Session.
  12. // Returns the Id for the new Client.
  13. func (c *Client) CreateStreamSession(dest string) (int32, string, error) {
  14. if dest == "" {
  15. dest = "TRANSIENT"
  16. }
  17. id := rand.Int31n(math.MaxInt32)
  18. r, err := c.sendCmd("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s %s\n", id, dest, c.allOptions())
  19. if err != nil {
  20. return -1, "", err
  21. }
  22. // TODO: move check into sendCmd()
  23. if r.Topic != "SESSION" || r.Type != "STATUS" {
  24. return -1, "", fmt.Errorf("Unknown Reply: %+v\n", r)
  25. }
  26. result := r.Pairs["RESULT"]
  27. if result != "OK" {
  28. return -1, "", ReplyError{ResultKeyNotFound, r}
  29. }
  30. return id, r.Pairs["DESTINATION"], nil
  31. }