initial API definition
This commit is contained in:
43
transport/transport.go
Normal file
43
transport/transport.go
Normal file
@ -0,0 +1,43 @@
|
||||
package transport
|
||||
|
||||
import "github.com/thesyncim/faye/message"
|
||||
|
||||
// handshake, connect, disconnect, subscribe, unsubscribe and publish
|
||||
|
||||
type Options struct {
|
||||
Url string
|
||||
InExt message.Extension
|
||||
outExt message.Extension
|
||||
//todo dial timeout
|
||||
//todo read/write deadline
|
||||
}
|
||||
|
||||
type Transport interface {
|
||||
Name() string
|
||||
Init(options *Options) error
|
||||
Options() *Options
|
||||
Handshake() error
|
||||
Connect() error
|
||||
Subscribe(subscription string, onMessage func(message *message.Message)) error
|
||||
Unsubscribe(subscription string) error
|
||||
Publish(subscription string, message *message.Message) error
|
||||
}
|
||||
|
||||
type Event string
|
||||
|
||||
const (
|
||||
Subscribe Event = "/meta/subscribe"
|
||||
Unsubscribe Event = "/meta/unsubscribe"
|
||||
Handshake Event = "/meta/handshake"
|
||||
Disconnect Event = "/meta/disconnect"
|
||||
)
|
||||
|
||||
var registeredTransports = map[string]Transport{}
|
||||
|
||||
func RegisterTransport(t Transport) {
|
||||
registeredTransports[t.Name()] = t //todo validate
|
||||
}
|
||||
|
||||
func GetTransport(name string) Transport {
|
||||
return registeredTransports[name]
|
||||
}
|
84
transport/websocket/websocket.go
Normal file
84
transport/websocket/websocket.go
Normal file
@ -0,0 +1,84 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/thesyncim/faye/message"
|
||||
"github.com/thesyncim/faye/transport"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func init() {
|
||||
transport.RegisterTransport(&Websocket{})
|
||||
}
|
||||
|
||||
type Websocket struct {
|
||||
TransportOpts *transport.Options
|
||||
conn *websocket.Conn
|
||||
clientID string
|
||||
msgID *uint64
|
||||
}
|
||||
|
||||
var _ transport.Transport = (*Websocket)(nil)
|
||||
|
||||
func (w *Websocket) Init(options *transport.Options) error {
|
||||
var (
|
||||
err error
|
||||
msgID uint64
|
||||
)
|
||||
|
||||
w.TransportOpts = options
|
||||
w.msgID = &msgID
|
||||
w.conn, _, err = websocket.DefaultDialer.Dial(options.Url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (w *Websocket) Name() string {
|
||||
return "websocket"
|
||||
}
|
||||
func (w *Websocket) nextMsgID() string {
|
||||
return strconv.Itoa(int(atomic.AddUint64(w.msgID, 1)))
|
||||
}
|
||||
|
||||
func (w *Websocket) Options() *transport.Options {
|
||||
return w.TransportOpts
|
||||
}
|
||||
func (w *Websocket) Handshake() (err error) {
|
||||
if err = w.conn.WriteJSON(append(nil, message.Message{
|
||||
Channel: string(transport.Handshake),
|
||||
Version: "1.0", //todo const
|
||||
SupportedConnectionTypes: []string{"websocket"},
|
||||
})); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var hsResps []message.Message
|
||||
if err = w.conn.ReadJSON(hsResps); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp := hsResps[0]
|
||||
if resp.GetError() != nil {
|
||||
return err
|
||||
}
|
||||
w.clientID = resp.ClientId
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Websocket) Connect() error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (w *Websocket) Subscribe(subscription string, onMessage func(message *message.Message)) error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (w *Websocket) Unsubscribe(subscription string) error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (w *Websocket) Publish(subscription string, message *message.Message) error {
|
||||
panic("not implemented")
|
||||
}
|
Reference in New Issue
Block a user