mirror of https://github.com/tidwall/tile38.git
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
|
/*
|
||
|
* Copyright (c) 2013 IBM Corp.
|
||
|
*
|
||
|
* All rights reserved. This program and the accompanying materials
|
||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||
|
* which accompanies this distribution, and is available at
|
||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||
|
*
|
||
|
* Contributors:
|
||
|
* Seth Hoenig
|
||
|
* Allan Stockdill-Mander
|
||
|
* Mike Robertson
|
||
|
*/
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"crypto/tls"
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
//"log"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
//MQTT.DEBUG = log.New(os.Stdout, "", 0)
|
||
|
//MQTT.ERROR = log.New(os.Stdout, "", 0)
|
||
|
stdin := bufio.NewReader(os.Stdin)
|
||
|
hostname, _ := os.Hostname()
|
||
|
|
||
|
server := flag.String("server", "tcp://127.0.0.1:1883", "The full URL of the MQTT server to connect to")
|
||
|
topic := flag.String("topic", hostname, "Topic to publish the messages on")
|
||
|
qos := flag.Int("qos", 0, "The QoS to send the messages at")
|
||
|
retained := flag.Bool("retained", false, "Are the messages sent with the retained flag")
|
||
|
clientid := flag.String("clientid", hostname+strconv.Itoa(time.Now().Second()), "A clientid for the connection")
|
||
|
username := flag.String("username", "", "A username to authenticate to the MQTT server")
|
||
|
password := flag.String("password", "", "Password to match username")
|
||
|
flag.Parse()
|
||
|
|
||
|
connOpts := MQTT.NewClientOptions().AddBroker(*server).SetClientID(*clientid).SetCleanSession(true)
|
||
|
if *username != "" {
|
||
|
connOpts.SetUsername(*username)
|
||
|
if *password != "" {
|
||
|
connOpts.SetPassword(*password)
|
||
|
}
|
||
|
}
|
||
|
tlsConfig := &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}
|
||
|
connOpts.SetTLSConfig(tlsConfig)
|
||
|
|
||
|
client := MQTT.NewClient(connOpts)
|
||
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||
|
fmt.Println(token.Error())
|
||
|
return
|
||
|
}
|
||
|
fmt.Printf("Connected to %s\n", *server)
|
||
|
|
||
|
for {
|
||
|
message, err := stdin.ReadString('\n')
|
||
|
if err == io.EOF {
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
client.Publish(*topic, byte(*qos), *retained, message)
|
||
|
}
|
||
|
}
|