2018-09-04 22:55:52 +00:00
|
|
|
package transport
|
|
|
|
|
2018-09-06 12:28:54 +00:00
|
|
|
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 {
|
2018-09-06 12:28:54 +00:00
|
|
|
Url string
|
|
|
|
RetryInterval time.Duration
|
|
|
|
|
2018-09-06 10:27:19 +00:00
|
|
|
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
|
2018-09-06 12:28:54 +00:00
|
|
|
Disconnect() error
|
2018-09-06 11:03:21 +00:00
|
|
|
Subscribe(subscription string, onMessage func(message message.Data)) error
|
2018-09-04 22:55:52 +00:00
|
|
|
Unsubscribe(subscription string) error
|
2018-09-06 11:03:21 +00:00
|
|
|
Publish(subscription string, message message.Data) error
|
2018-09-04 22:55:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 12:28:54 +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 (
|
2018-09-06 12:28:54 +00:00
|
|
|
//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
|
|
|
)
|
|
|
|
|
2018-09-06 12:28:54 +00:00
|
|
|
var MetaEvents = []Meta{MetaSubscribe, MetaConnect, MetaUnsubscribe, MetaHandshake, MetaDisconnect}
|
2018-09-05 13:17:11 +00:00
|
|
|
|
2018-09-06 12:28:54 +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]
|
|
|
|
}
|