This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/tg/broker.go
2016-02-09 10:21:37 +00:00

42 lines
722 B
Go

package tg
import (
"encoding/json"
"fmt"
"log"
"net"
)
type Broker struct {
Socket net.Conn
}
func ConnectToBroker(brokerAddr string) (*Broker, error) {
sock, err := net.Dial("tcp", brokerAddr)
if err != nil {
return nil, err
}
broker := new(Broker)
broker.Socket = sock
return broker, nil
}
func (b *Broker) Close() {
b.Socket.Close()
}
func (b *Broker) SendTextMessage(chat *APIChat, text string) {
cmd := ClientCommand{
Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{
Text: text,
},
}
// Encode command and send to broker
data, err := json.Marshal(cmd)
if err != nil {
log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error())
}
fmt.Fprintln(data)
}