fayec/transport/transport.go

61 lines
1.9 KiB
Go
Raw Normal View History

2018-09-04 22:55:52 +00:00
package transport
import (
"github.com/thesyncim/faye/message"
2018-09-11 11:30:07 +00:00
"net/http"
"time"
)
2018-09-04 22:55:52 +00:00
2018-09-06 14:31:05 +00:00
//Options represents the connection options to be used by a transport
2018-09-04 22:55:52 +00:00
type Options struct {
2018-09-11 11:30:07 +00:00
Headers http.Header
Cookies http.CookieJar
2018-09-11 11:30:07 +00:00
MaxRetries int
2018-09-11 11:30:07 +00:00
RetryInterval time.Duration
DialDeadline time.Duration
ReadDeadline time.Duration
WriteDeadline time.Duration
2018-09-04 22:55:52 +00:00
}
2018-09-06 14:31:05 +00:00
//Transport represents the transport to be used to comunicate with the faye server
2018-09-04 22:55:52 +00:00
type Transport interface {
//name returns the transport name
2018-09-04 22:55:52 +00:00
Name() string
2018-09-06 14:31:05 +00:00
//Init initializes the transport with the provided options
Init(endpoint string, options *Options) error
2018-09-06 14:31:05 +00:00
//Options return the transport Options
2018-09-04 22:55:52 +00:00
Options() *Options
2018-09-06 14:31:05 +00:00
//Handshake initiates a connection negotiation by sending a message to the /meta/handshake channel.
Handshake(msg *message.Message) (*message.Message, error)
//Init is called after a client has discovered the servers capabilities with a handshake exchange,
2018-09-06 14:31:05 +00:00
//a connection is established by sending a message to the /meta/connect channel
Connect(msg *message.Message) error
2018-09-06 14:31:05 +00:00
//Disconnect closes all subscriptions and inform the server to remove any client-related state.
//any subsequent method call to the transport object will result in undefined behaviour.
Disconnect(msg *message.Message) error
//SendMessage sens a message through the transport
SendMessage(msg *message.Message) error
2018-09-06 13:29:49 +00:00
SetOnMessageReceivedHandler(onMsg func(msg *message.Message))
2018-09-06 13:29:49 +00:00
//SetOnTransportUpHandler is called when the transport is connected
SetOnTransportUpHandler(callback func())
2018-09-05 13:17:11 +00:00
//SetOnTransportDownHandler is called when the transport goes down
SetOnTransportDownHandler(callback func(error))
2018-09-06 13:29:49 +00:00
//handled by dispatcher
SetOnErrorHandler(onError func(err error))
2018-09-06 13:29:49 +00:00
}
2018-09-04 22:55:52 +00:00
var registeredTransports = map[string]Transport{}
func RegisterTransport(t Transport) {
registeredTransports[t.Name()] = t //todo validate
}
func GetTransport(name string) Transport {
return registeredTransports[name]
}