main.go 903 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. pb "app.yhyue.com/BP/queued/proto"
  4. "app.yhyue.com/BP/queued/util"
  5. "github.com/golang/protobuf/proto"
  6. "log"
  7. "math/rand"
  8. "time"
  9. )
  10. const (
  11. address = "127.0.0.1:8080"
  12. )
  13. var (
  14. sendPool = make(chan *pb.PubReq, 50)
  15. recivePool = make(chan *pb.PubReq, 50)
  16. )
  17. func makeData() []float32 {
  18. ret := make([]float32, 0, 0)
  19. for i := 0; i < 10; i++ {
  20. ret = append(ret, rand.Float32())
  21. }
  22. return ret
  23. }
  24. func main() {
  25. pub := util.NewPublisher(address, "syslog", sendPool)
  26. rec := util.NewReciver(address, "syslog", recivePool)
  27. go pub.Run()
  28. go rec.Run()
  29. //发送消息
  30. go func() {
  31. for {
  32. param := pb.ClfReq{Item: makeData()}
  33. reqParam, _ := proto.Marshal(&param)
  34. time.Sleep(2 * time.Second)
  35. sendPool <- &pb.PubReq{
  36. PublishType: 2,
  37. Param: reqParam,
  38. }
  39. }
  40. }()
  41. //接受消息
  42. for {
  43. select {
  44. case msg := <-recivePool:
  45. log.Println(msg)
  46. }
  47. }
  48. }