├── LICENSE ├── README.md └── gochat-mqtt.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Al S-M 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gochat-mqtt 2 | =========== 3 | 4 | A simple chat program to demonstrate [MQTT](http://mqtt.org) with Go, using the [Eclipse Paho](http://eclipse.org/paho) Go client 5 | 6 | **Build** 7 | 8 | Requires Go, obviously... 9 | 10 | Set the `GOPATH` and `GOBIN` variables appropriately, then... 11 | 12 | ``` 13 | $ go get git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git 14 | $ go install gochat-mqtt.go 15 | ``` 16 | 17 | **Run** 18 | 19 | ``` 20 | $ gochat-mqtt --help 21 | Usage of gochat-mqtt: 22 | -name="user201": Username to be displayed 23 | -room="gochat": The chat room to enter. default 'gochat' 24 | -server="tcp://iot.eclipse.org:1883": The MQTT server to connect to 25 | ``` 26 | -------------------------------------------------------------------------------- /gochat-mqtt.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "flag" 6 | "fmt" 7 | MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git" 8 | "io" 9 | "math/rand" 10 | "os" 11 | "strconv" 12 | "strings" 13 | "time" 14 | ) 15 | 16 | func messageReceived(client *MQTT.Client, msg MQTT.Message) { 17 | topics := strings.Split(msg.Topic(), "/") 18 | msgFrom := topics[len(topics)-1] 19 | fmt.Print(msgFrom + ": " + string(msg.Payload())) 20 | } 21 | 22 | func main() { 23 | stdin := bufio.NewReader(os.Stdin) 24 | rand.Seed(time.Now().Unix()) 25 | 26 | server := flag.String("server", "tcp://iot.eclipse.org:1883", "The MQTT server to connect to") 27 | room := flag.String("room", "gochat", "The chat room to enter. default 'gochat'") 28 | name := flag.String("name", "user"+strconv.Itoa(rand.Intn(1000)), "Username to be displayed") 29 | flag.Parse() 30 | 31 | subTopic := strings.Join([]string{"/gochat/", *room, "/+"}, "") 32 | pubTopic := strings.Join([]string{"/gochat/", *room, "/", *name}, "") 33 | 34 | opts := MQTT.NewClientOptions().AddBroker(*server).SetClientID(*name).SetCleanSession(true) 35 | 36 | opts.OnConnect = func(c *MQTT.Client) { 37 | if token := c.Subscribe(subTopic, 1, messageReceived); token.Wait() && token.Error() != nil { 38 | panic(token.Error()) 39 | } 40 | } 41 | 42 | client := MQTT.NewClient(opts) 43 | 44 | if token := client.Connect(); token.Wait() && token.Error() != nil { 45 | panic(token.Error()) 46 | } 47 | fmt.Printf("Connected as %s to %s\n", *name, *server) 48 | 49 | for { 50 | message, err := stdin.ReadString('\n') 51 | if err == io.EOF { 52 | os.Exit(0) 53 | } 54 | if token := client.Publish(pubTopic, 1, false, message); token.Wait() && token.Error() != nil { 55 | fmt.Println("Failed to send message") 56 | } 57 | } 58 | } 59 | --------------------------------------------------------------------------------