func Dial(o Opts) (*Client, error) { if o.Logger == nil { o.Logger = log.New(ioutil.Discard, "", log.LstdFlags) } conn, err := stomp.Dial(o.Network, o.Addr, o.Opts) if err != nil { return nil, err } reqs, err := conn.SubscribeWithHeaders(o.Requests.Name, o.Requests.AckMode, o.Requests.Headers) if err != nil { return nil, err } resp, err := conn.SubscribeWithHeaders(o.Responses.Name, o.Responses.AckMode, o.Responses.Headers) if err != nil { return nil, err } c := Client{ I: make(chan Response), O: make(chan Request), l: o.Logger, conn: conn, requests: reqs, responses: resp, } go c.receiver() go c.responder() return &c, nil }
func sendMessages(msg string) { defer func() { stop <- true }() conn, err := stomp.Dial("tcp", *serverAddr, options) if err != nil { println("cannot connect to server", err.Error()) return } err = conn.Send(*queueName, "text/plain", []byte(msg), nil) if err != nil { println("failed to send to server", err) return } }
func main() { conn, err := stomp.Dial("tcp", "192.168.1.108:61613", stomp.Options{}) if err != nil { fmt.Println(err) return } req := Request{From: "[email protected]/%23random", Message: "!roll 2d6+5"} var b bytes.Buffer w := bufio.NewWriter(&b) m := json.NewEncoder(w) err = m.Encode(req) if err != nil { fmt.Println(err) return } w.Flush() t := time.NewTicker(time.Second * 10) LOOP: for { select { case <-t.C: fmt.Println(req) fmt.Println(b) err = conn.Send("/topic/prayers", "", b.Bytes(), nil) if err != nil { fmt.Println(err) break LOOP } } } conn.Disconnect() }
func main() { conn, err := stomp.Dial("tcp", "192.168.1.108:61613", stomp.Options{NonStandard: stomp.NewHeader("client-id", "ladyluck")}) if err != nil { fmt.Println(err) return } sub, err := conn.SubscribeWithHeaders("/topic/prayers", stomp.AckAuto, stomp.NewHeader()) if err != nil { fmt.Println(err) return } regex, err := regexp.Compile(`(\d+)?d(\d+)((\+|\-)?\d+)?`) if err != nil { fmt.Println(err) return } var req queues.Request LOOP: for { select { case msg := <-sub.C: fmt.Println(msg.Body) d := json.NewDecoder(bytes.NewBuffer(msg.Body)) err = d.Decode(&req) if err != nil { fmt.Println(fmt.Errorf("Unable to decode body: %s", err)) break } if len(req.Message) == 0 { break } parts := strings.Split(req.Message, " ") if len(parts) < 2 { break } if strings.Compare("!roll", parts[0]) != 0 { break } var isHelp, doesMatch bool isHelp = strings.Compare("help", strings.ToLower(parts[1])) == 0 doesMatch = regex.MatchString(parts[1]) if !isHelp && !doesMatch { break } var resp queues.Response if isHelp { resp = Response{From: "ladyluck@[email protected]", To: req.From, Message: "!roll usage: !roll <cmd|dice-formula> [OPTIONS]+"} } if doesMatch { resp = Response{From: "ladyluck@[email protected]", To: req.From, Message: "xguzman: you rolled 2d6+5 - 3+1 = 4"} } fmt.Println(resp) var b bytes.Buffer w := bufio.NewWriter(&b) e := json.NewEncoder(w) e.Encode(resp) w.Flush() fmt.Printf("RESPONSE: %s\n", resp) err = conn.Send("/topic/answers", "", b.Bytes(), nil) if err != nil { fmt.Println(fmt.Errorf("Unable to send resp: %+v %s", resp, err)) break LOOP } } } conn.Disconnect() }