fayec/transport/transport.go

79 lines
2.1 KiB
Go
Raw Normal View History

2018-09-04 22:55:52 +00:00
package transport
import (
"github.com/thesyncim/faye/message"
"time"
)
2018-09-04 22:55:52 +00:00
// handshake, connect, disconnect, subscribe, unsubscribe and publish
type Options struct {
Url string
RetryInterval time.Duration
InExt []message.Extension
OutExt []message.Extension
2018-09-04 22:55:52 +00:00
//todo dial timeout
//todo read/write deadline
}
type Transport interface {
Name() string
Init(options *Options) error
Options() *Options
Handshake() error
Connect() error
Disconnect() error
Subscribe(subscription string, onMessage func(message message.Data)) error
2018-09-04 22:55:52 +00:00
Unsubscribe(subscription string) error
Publish(subscription string, message message.Data) error
2018-09-04 22:55:52 +00:00
}
type Meta = string
const (
MetaSubscribe Meta = "/meta/subscribe"
MetaConnect Meta = "/meta/connect"
MetaDisconnect Meta = "/meta/disconnect"
MetaUnsubscribe Meta = "/meta/unsubscribe"
MetaHandshake Meta = "/meta/handshake"
)
type Reconnect = string
2018-09-04 22:55:52 +00:00
const (
//ReconnectRetry indicates that a client MAY attempt to reconnect with a /meta/connect message,
//after the interval (as defined by interval advice field or client-default backoff), and with the same credentials.
ReconnectRetry Reconnect = "retry"
//ReconnectHandshake indicates that the server has terminated any prior connection status and the client MUST reconnect
// with a /meta/handshake message.
//A client MUST NOT automatically retry when a reconnect advice handshake has been received.
ReconnectHandshake Reconnect = "handshake"
//ReconnectNone indicates a hard failure for the connect attempt.
//A client MUST respect reconnect advice none and MUST NOT automatically retry or handshake.
ReconnectNone Reconnect = "none"
2018-09-04 22:55:52 +00:00
)
var MetaEvents = []Meta{MetaSubscribe, MetaConnect, MetaUnsubscribe, MetaHandshake, MetaDisconnect}
2018-09-05 13:17:11 +00:00
func IsMetaEvent(channel string) bool {
for i := range MetaEvents {
if channel == MetaEvents[i] {
2018-09-05 13:17:11 +00:00
return true
}
}
return false
}
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]
}