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:01:05 +00:00

42 lines
746 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
err := json.NewEncoder(b.Socket).Encode(&cmd)
if err != nil {
log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error())
}
fmt.Fprintf(b.Socket, "\n")
}