Refactor url client connection

This commit is contained in:
James Mills 2017-08-19 22:09:36 -07:00
parent e5a771bae3
commit e01ed2ab3a
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 28 additions and 20 deletions

View File

@ -6,6 +6,8 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"
"golang.org/x/net/websocket"
@ -23,8 +25,7 @@ const (
// Client ...
type Client struct {
host string
port int
url string
retry time.Duration
reconnect time.Duration
@ -39,16 +40,15 @@ type Options struct {
}
// NewClient ...
func NewClient(host string, port int, options *Options) *Client {
func NewClient(url string, options *Options) *Client {
var (
reconnectInterval = DefaultReconnectInterval
retryInterval = DefaultRetryInterval
)
client := &Client{
host: host,
port: port,
}
url = strings.TrimSuffix(url, "/")
client := &Client{url: url}
if options != nil {
if options.ReconnectInterval != 0 {
@ -78,13 +78,16 @@ func (c *Client) Handle(msg *msgbus.Message) {
func (c *Client) Pull(topic string) {
var msg *msgbus.Message
url := fmt.Sprintf("http://%s:%d/%s", c.host, c.port, topic)
url := fmt.Sprintf("%s/%s", c.url, topic)
client := &http.Client{}
for {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("error constructing pull request to %s: %s", url, err)
log.Printf(
"error constructing pull request to %s: %s",
url, err,
)
time.Sleep(c.retry)
continue
}
@ -121,7 +124,7 @@ func (c *Client) Publish(topic, message string) error {
payload.Write([]byte(message))
url := fmt.Sprintf("http://%s:%d/%s", c.host, c.port, topic)
url := fmt.Sprintf("%s/%s", c.url, topic)
client := &http.Client{}
@ -171,10 +174,15 @@ func (s *Subscriber) Run() {
origin := "http://localhost/"
url := fmt.Sprintf(
"ws://%s:%d/%s",
s.client.host, s.client.port, s.topic,
)
u, err := url.Parse(s.client.url)
if err != nil {
log.Fatal("invalid url: %s", s.client.url)
}
u.Scheme = "ws"
u.Path += fmt.Sprintf("/%s", s.topic)
url := u.String()
for {
s.conn, err = websocket.Dial(url, "", origin)

View File

@ -14,16 +14,16 @@ import (
const defaultTopic = "hello"
func main() {
var (
host string
port int
var url string
flag.StringVar(
&url, "url",
"http://localhost:8000", "url to connect to",
)
flag.StringVar(&host, "host", "localhost", "host to connect to")
flag.IntVar(&port, "port", 8000, "port to connect to")
flag.Parse()
client := client.NewClient(host, port, nil)
client := client.NewClient(url, nil)
if flag.Arg(0) == "sub" {
subscribe(client, flag.Arg(1))