Working authenticated connection
This commit is contained in:
@ -1,10 +1,13 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/thesyncim/faye/message"
|
||||
"github.com/thesyncim/faye/transport"
|
||||
"github.com/thesyncim/fayec/message"
|
||||
"github.com/thesyncim/fayec/transport"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
@ -15,7 +18,7 @@ func init() {
|
||||
transport.RegisterTransport(&Websocket{})
|
||||
}
|
||||
|
||||
//Websocket represents an websocket transport for the faye protocol
|
||||
// Websocket represents an websocket transport for the faye protocol
|
||||
type Websocket struct {
|
||||
topts *transport.Options
|
||||
|
||||
@ -34,7 +37,7 @@ type Websocket struct {
|
||||
|
||||
var _ transport.Transport = (*Websocket)(nil)
|
||||
|
||||
//Init initializes the transport with the provided options
|
||||
// Init initializes the transport with the provided options
|
||||
func (w *Websocket) Init(endpoint string, options *transport.Options) error {
|
||||
var (
|
||||
err error
|
||||
@ -43,20 +46,18 @@ func (w *Websocket) Init(endpoint string, options *transport.Options) error {
|
||||
|
||||
w.stopCh = make(chan error)
|
||||
w.conn, _, err = websocket.DefaultDialer.Dial(endpoint, options.Headers)
|
||||
err = w.conn.UnderlyingConn().(*tls.Conn).NetConn().(*net.TCPConn).SetKeepAlive(true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.conn.SetPingHandler(func(appData string) error {
|
||||
return w.conn.WriteJSON(make([]struct{}, 0))
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//Init initializes the transport with the provided options
|
||||
// Init initializes the transport with the provided options
|
||||
func (w *Websocket) SetOnErrorHandler(onError func(err error)) {
|
||||
w.onError = onError
|
||||
}
|
||||
@ -67,20 +68,26 @@ func (w *Websocket) readWorker() error {
|
||||
case err := <-w.stopCh:
|
||||
return err
|
||||
default:
|
||||
}
|
||||
var payload []message.Message
|
||||
err := w.conn.ReadJSON(&payload)
|
||||
if err != nil {
|
||||
var payload []message.Message
|
||||
_, data, err := w.conn.ReadMessage()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//dispatch
|
||||
msg := &payload[0]
|
||||
w.onMsg(msg)
|
||||
|
||||
return err
|
||||
}
|
||||
//dispatch
|
||||
msg := &payload[0]
|
||||
w.onMsg(msg)
|
||||
}
|
||||
}
|
||||
|
||||
//name returns the transport name (websocket)
|
||||
// name returns the transport name (websocket)
|
||||
func (w *Websocket) Name() string {
|
||||
return transportName
|
||||
}
|
||||
@ -105,12 +112,12 @@ again: //todo move this to scheduler
|
||||
return nil
|
||||
}
|
||||
|
||||
//Options return the transport Options
|
||||
// Options return the transport Options
|
||||
func (w *Websocket) Options() *transport.Options {
|
||||
return w.topts
|
||||
}
|
||||
|
||||
//Handshake initiates a connection negotiation by sending a message to the /meta/handshake channel.
|
||||
// Handshake initiates a connection negotiation by sending a message to the /meta/handshake channel.
|
||||
func (w *Websocket) Handshake(msg *message.Message) (resp *message.Message, err error) {
|
||||
err = w.SendMessage(msg)
|
||||
|
||||
@ -144,8 +151,8 @@ func (w *Websocket) SetOnTransportUpHandler(onTransportUp func()) {
|
||||
w.onTransportUp = onTransportUp
|
||||
}
|
||||
|
||||
//Disconnect closes all subscriptions and inform the server to remove any client-related state.
|
||||
//any subsequent method call to the client object will result in undefined behaviour.
|
||||
// Disconnect closes all subscriptions and inform the server to remove any client-related state.
|
||||
// any subsequent method call to the client object will result in undefined behaviour.
|
||||
func (w *Websocket) Disconnect(m *message.Message) error {
|
||||
w.stopCh <- nil
|
||||
close(w.stopCh)
|
||||
|
Reference in New Issue
Block a user