func client(i int) { log.Print("starting client ", i) conn, err := net.Dial("tcp", *host) if err != nil { log.Fatal("dial: ", err) } cc := mqtt.NewClientConn(conn) cc.Dump = *dump if err := cc.Connect(*user, *pass); err != nil { log.Fatal("connect: %v\n", err) os.Exit(1) } half := int32(*pace / 2) for { cc.Publish(&proto.Publish{ Header: proto.Header{}, TopicName: topic, Payload: payload, }) sltime := rand.Int31n(half) - (half / 2) + int32(*pace) time.Sleep(time.Duration(sltime) * time.Second) } }
func main() { flag.Parse() if flag.NArg() != 2 { fmt.Fprintln(os.Stderr, "usage: pub topic message") return } conn, err := net.Dial("tcp", *host) if err != nil { fmt.Fprintf(os.Stderr, "dial: ", err) return } cc := mqtt.NewClientConn(conn) cc.Dump = *dump if err := cc.Connect(*user, *pass); err != nil { fmt.Fprintf(os.Stderr, "connect: %v\n", err) os.Exit(1) } fmt.Println("Connected with client id ", cc.ClientId) cc.Publish(&proto.Publish{ Header: proto.Header{Retain: *retain}, TopicName: flag.Arg(0), Payload: proto.BytesPayload([]byte(flag.Arg(1))), }) if *wait { <-make(chan bool) } cc.Disconnect() }
func connect() *mqtt.ClientConn { conn, err := net.Dial("tcp", *host) if err != nil { fmt.Fprintf(os.Stderr, "dial: %v\n", err) os.Exit(2) } cc := mqtt.NewClientConn(conn) cc.Dump = *dump cc.ClientId = *id err = cc.Connect(*user, *pass) if err != nil { fmt.Println(err) os.Exit(3) } cwg.Done() return cc }
func main() { flag.Parse() if flag.NArg() < 1 { fmt.Fprintln(os.Stderr, "usage: sub topic [topic topic...]") return } conn, err := net.Dial("tcp", *host) if err != nil { fmt.Fprintf(os.Stderr, "dial: ", err) return } cc := mqtt.NewClientConn(conn) cc.Dump = *dump cc.ClientId = *id tq := make([]proto.TopicQos, flag.NArg()) for i := 0; i < flag.NArg(); i++ { tq[i].Topic = flag.Arg(i) tq[i].Qos = proto.QosAtMostOnce } if err := cc.Connect(*user, *pass); err != nil { fmt.Fprintf(os.Stderr, "connect: %v\n", err) os.Exit(1) } fmt.Println("Connected with client id ", cc.ClientId) cc.Subscribe(tq) for m := range cc.Incoming { fmt.Print(m.TopicName, "\t") m.Payload.WritePayload(os.Stdout) fmt.Println("\tr: ", m.Header.Retain) } }