p2p-health-bot/receiver.go

34 lines
749 B
Go
Raw Normal View History

2018-05-24 15:32:39 +00:00
package main
import (
"log"
"github.com/status-im/status-go-sdk"
)
func startReceiver(ch *sdk.Channel) {
if _, err := ch.Subscribe(func(m *sdk.Msg) {
2018-06-28 11:48:36 +00:00
log.Printf("[DEBUG] Got message: %v", m)
2018-05-24 15:32:39 +00:00
rawmsg, ok := m.Properties.(*sdk.PublishMsg)
if !ok {
2018-06-28 11:48:36 +00:00
log.Printf("[ERROR] Wrong message props type received: %T", m.Properties)
2018-05-24 15:32:39 +00:00
return
}
msg := Msg(rawmsg.Text)
if msg.IsRequest() {
counter, err := msg.Counter()
if err != nil {
2018-06-28 11:48:36 +00:00
log.Printf("[ERROR] Can't extract counter: %v", err)
2018-05-24 15:32:39 +00:00
}
go func(counter int) {
var body = NewResponseMsg(counter)
if err := ch.Publish(string(body)); err != nil {
2018-06-28 11:48:36 +00:00
log.Printf("[ERROR] Can't send response: %v", err)
2018-05-24 15:32:39 +00:00
}
}(counter)
}
}); err != nil {
log.Fatal(err)
}
}