func (client *Client) ReceiveMessage(queueName string, waitSeconds int, peekOnly bool) chan *MsgReceiveWithError {
msgChan := make(chan *MsgReceiveWithError)
go func() {
for {
req := &request{
method: http.MethodGet,
path: fmt.Sprintf("/queues/%s/messages", queueName),
contentType: ContentTypeXML,
params: map[string]string{},
}
if waitSeconds != 0 {
req.params["waitseconds"] = strconv.Itoa(waitSeconds)
}
if peekOnly {
req.params["peekonly"] = strconv.FormatBool(true)
}
resp, err := client.doRequest(req)
if err != nil {
msgChan <- &MsgReceiveWithError{Err: err}
continue
}
if resp.StatusCode == 404 {
resp.Body.Close()
continue
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
msgChan <- &MsgReceiveWithError{Err: err}
resp.Body.Close()
continue
}
msg := &MsgReceive{}
err = xml.Unmarshal(data, msg)
if err != nil {
msgChan <- &MsgReceiveWithError{Err: err}
resp.Body.Close()
continue
}
msgChan <- &MsgReceiveWithError{Msg: msg}
resp.Body.Close() //有可能执行不了吧
}
}()
return msgChan //若msgChan里没有东西的话就阻塞
}