glpclient

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2021 License: MIT Imports: 8 Imported by: 0

README

golongpoll client GoDoc

Basic usage

You will first need to create the client configuring it with the URL of the golongpoll server. Each client must be started after being instantiated.

When an event is received, it will be sent in the EventsChan of the client.

import (
  "fmt" 

  "github.com/jcuga/golongpoll/go-client"
)

u, _ := url.Parse("http://127.0.0.1/events")

// Second argument is the category on which we want to receive events
c := glpclient.NewClient(u, "news")
c.Start()

for {
  select {
  case e := <-c.EventsChan:
    fmt.Printf("Got event %s", string(e.Data))
  }
}

c.Stop()

Options

  • Timeout: Defines the timeout used when calling the longpoll server
  • Reattempt: Defines the amount of time the client waits after an unsuccessful connection.
  • BasicAuthUsername: When defined along with the BasicAuthPassword, it will use the credentials in basic HTTP authentication
  • BasicAuthPassword: When defined along with the BasicAuthUsername, it will use the credentials in basic HTTP authentication

The client's HTTP client can be overiden. Example:

u, _ := url.Parse("http://127.0.0.1/events")
c := glpclient.NewClient(u, "news")

tr := &http.Transport{
	MaxIdleConns:       10,
	IdleConnTimeout:    30 * time.Second,
	DisableCompression: true,
}
c.HttpClient= &http.Client{Transport: tr}

...

Receiving JSON

The Data attribute of the received events is a *json.RawMessage that can be used with json.Unmarshal. Example:

e := <-c.EventsChan
var data map[string]string
err := json.Unmarshal(e.Data, &data)
if err != nil {
  t.Error("Error while decoding event from channel")
}

Documentation

Index

Constants

View Source
const (
	// DEFAULT_TIMEOUT is the default timeout in seconds (30) used when calling the longpoll server.
	DEFAULT_TIMEOUT = 30
	// DEFAULT_REATTEMPT is the default amount of time in seconds (30) the client waits to reconnect to the server after a failure.
	DEFAULT_REATTEMPT = 30
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {

	// Timeout controls the timeout in all the requests, can be changed after instantiating the client
	Timeout uint64
	// Reattempt controls the amount of time the client waits to reconnect to the server after a failure
	Reattempt time.Duration
	// Will get all the events data
	EventsChan chan PollEvent

	// The HTTP client to perform the requests, any changes on this should be done prior to starting the client the first time
	HttpClient *http.Client
	// The username to be used for basic HTTP authentication
	BasicAuthUsername string
	// The password to be used for basic HTTP authentication
	BasicAuthPassword string

	// Whether or not logging should be enabled
	LoggingEnabled bool
	// contains filtered or unexported fields
}

Client will connect to a given URL and send received events into a channel.

func NewClient

func NewClient(url *url.URL, category string) *Client

NewClient instantiate a new client to connect to a given URL and send the events into a channel The URL shouldn't contain any GET parameters although its fine if it contains some but category, since_time and timeout will be overriten stubChanData must either be an empty structure of the events data or a map[string]interface{} if the events do not follow a specific structure

func (*Client) Start

func (c *Client) Start()

Start the polling of the events on the URL defined in the client Will send the events in the EventsChan of the client

func (*Client) Stop

func (c *Client) Stop()

Stop causes the client's goroutine to stop listening for events and exit.

type PollEvent

type PollEvent struct {
	// Timestamp is milliseconds since epoch to match javascrits Date.getTime()
	Timestamp int64  `json:"timestamp"`
	Category  string `json:"category"`
	// NOTE: Data can be anything that is able to passed to json.Marshal()
	Data json.RawMessage `json:"data"`
}

PollEvent is taken from https://github.com/jcuga/golongpoll/blob/master/events.go

type PollResponse

type PollResponse struct {
	Events    []PollEvent `json:"events"`
	Timestamp int64       `json:"timestamp"`
}

PollResponse contains any events and the most recent event timestamp.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL