2018-09-04 22:55:52 +00:00
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
|
|
import (
|
2018-09-06 10:27:19 +00:00
|
|
|
|
"fmt"
|
2018-09-04 22:55:52 +00:00
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
"github.com/thesyncim/faye/message"
|
2018-09-07 12:32:26 +00:00
|
|
|
|
"github.com/thesyncim/faye/subscription"
|
2018-09-04 22:55:52 +00:00
|
|
|
|
"github.com/thesyncim/faye/transport"
|
2018-09-07 12:32:26 +00:00
|
|
|
|
"log"
|
2018-09-06 10:27:19 +00:00
|
|
|
|
|
2018-09-04 22:55:52 +00:00
|
|
|
|
"strconv"
|
2018-09-05 12:56:32 +00:00
|
|
|
|
"sync"
|
2018-09-04 22:55:52 +00:00
|
|
|
|
"sync/atomic"
|
|
|
|
|
)
|
|
|
|
|
|
2018-09-04 23:22:43 +00:00
|
|
|
|
const transportName = "websocket"
|
|
|
|
|
|
2018-09-04 22:55:52 +00:00
|
|
|
|
func init() {
|
|
|
|
|
transport.RegisterTransport(&Websocket{})
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Websocket represents an websocket transport for the faye protocol
|
2018-09-04 22:55:52 +00:00
|
|
|
|
type Websocket struct {
|
2018-09-11 10:09:30 +00:00
|
|
|
|
topts *transport.Options
|
2018-09-06 15:59:56 +00:00
|
|
|
|
|
|
|
|
|
connMu sync.Mutex
|
|
|
|
|
conn *websocket.Conn
|
|
|
|
|
clientID string
|
|
|
|
|
msgID *uint64
|
|
|
|
|
once sync.Once
|
|
|
|
|
advice atomic.Value //type message.Advise
|
2018-09-05 12:56:32 +00:00
|
|
|
|
|
2018-09-06 13:29:49 +00:00
|
|
|
|
stopCh chan error
|
2018-09-06 12:28:54 +00:00
|
|
|
|
|
2018-09-07 12:32:26 +00:00
|
|
|
|
//subsMu sync.Mutex //todo sync.Map
|
|
|
|
|
//subs map[string]chan *message.Message
|
|
|
|
|
|
|
|
|
|
subsMu2 sync.Mutex //todo sync.Map
|
|
|
|
|
subs2 map[string][]*subscription.Subscription
|
2018-09-06 13:29:49 +00:00
|
|
|
|
|
|
|
|
|
onPubResponseMu sync.Mutex //todo sync.Map
|
|
|
|
|
onPublishResponse map[string]func(message *message.Message)
|
2018-09-04 22:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ transport.Transport = (*Websocket)(nil)
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Init initializes the transport with the provided options
|
2018-09-11 10:09:30 +00:00
|
|
|
|
func (w *Websocket) Init(endpoint string, options *transport.Options) error {
|
2018-09-04 22:55:52 +00:00
|
|
|
|
var (
|
|
|
|
|
err error
|
|
|
|
|
msgID uint64
|
|
|
|
|
)
|
2018-09-11 10:09:30 +00:00
|
|
|
|
w.topts = options
|
2018-09-04 22:55:52 +00:00
|
|
|
|
w.msgID = &msgID
|
2018-09-07 12:32:26 +00:00
|
|
|
|
//w.subs = map[string]chan *message.Message{}
|
|
|
|
|
w.subs2 = map[string][]*subscription.Subscription{}
|
2018-09-06 15:59:56 +00:00
|
|
|
|
w.onPublishResponse = map[string]func(message *message.Message){}
|
2018-09-06 13:29:49 +00:00
|
|
|
|
w.stopCh = make(chan error)
|
2018-09-11 10:09:30 +00:00
|
|
|
|
w.conn, _, err = websocket.DefaultDialer.Dial(endpoint, nil)
|
2018-09-04 22:55:52 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-09-05 12:56:32 +00:00
|
|
|
|
|
|
|
|
|
func (w *Websocket) readWorker() error {
|
|
|
|
|
for {
|
2018-09-06 12:28:54 +00:00
|
|
|
|
select {
|
2018-09-06 13:29:49 +00:00
|
|
|
|
case err := <-w.stopCh:
|
|
|
|
|
return err
|
2018-09-06 12:28:54 +00:00
|
|
|
|
default:
|
|
|
|
|
}
|
2018-09-06 09:16:36 +00:00
|
|
|
|
var payload []message.Message
|
2018-09-05 12:56:32 +00:00
|
|
|
|
err := w.conn.ReadJSON(&payload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//dispatch
|
2018-09-06 10:27:19 +00:00
|
|
|
|
msg := &payload[0]
|
2018-09-11 10:09:30 +00:00
|
|
|
|
w.topts.Extensions.ApplyInExtensions(msg)
|
2018-09-05 13:17:11 +00:00
|
|
|
|
|
2018-09-06 11:23:53 +00:00
|
|
|
|
if msg.Advice != nil {
|
|
|
|
|
w.handleAdvise(msg.Advice)
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 13:29:49 +00:00
|
|
|
|
if transport.IsMetaMessage(msg) {
|
2018-09-05 13:23:03 +00:00
|
|
|
|
//handle it
|
|
|
|
|
switch msg.Channel {
|
2018-09-06 12:28:54 +00:00
|
|
|
|
case transport.MetaSubscribe:
|
|
|
|
|
//handle MetaSubscribe resp
|
2018-09-07 12:32:26 +00:00
|
|
|
|
w.subsMu2.Lock()
|
|
|
|
|
subscriptions, ok := w.subs2[msg.Subscription]
|
2018-09-07 13:00:35 +00:00
|
|
|
|
if !ok {
|
|
|
|
|
panic("BUG: subscription not registered `" + msg.Subscription + "`")
|
|
|
|
|
}
|
2018-09-05 13:45:53 +00:00
|
|
|
|
if !msg.Successful {
|
2018-09-06 16:40:00 +00:00
|
|
|
|
if msg.GetError() == nil {
|
|
|
|
|
//inject the error if the server returns unsuccessful without error
|
2018-09-06 10:27:19 +00:00
|
|
|
|
msg.Error = fmt.Sprintf("susbscription `%s` failed", msg.Subscription)
|
|
|
|
|
}
|
2018-09-07 12:32:26 +00:00
|
|
|
|
var si = -1
|
|
|
|
|
for i := range subscriptions {
|
|
|
|
|
if subscriptions[i].ID() == msg.Id {
|
|
|
|
|
si = i
|
|
|
|
|
select {
|
|
|
|
|
case subscriptions[i].SubscriptionResult() <- msg.GetError():
|
|
|
|
|
close(subscriptions[i].MsgChannel())
|
2018-09-07 13:00:35 +00:00
|
|
|
|
default:
|
2018-09-07 12:32:26 +00:00
|
|
|
|
log.Println("subscription has no listeners") //todo remove*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//remove subscription
|
|
|
|
|
if si > -1 {
|
|
|
|
|
subscriptions = subscriptions[:si+copy(subscriptions[si:], subscriptions[si+1:])]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.subs2[msg.Subscription] = subscriptions
|
|
|
|
|
//v2
|
|
|
|
|
} else {
|
|
|
|
|
for i := range subscriptions {
|
|
|
|
|
if subscriptions[i].ID() == msg.Id {
|
|
|
|
|
select {
|
|
|
|
|
case subscriptions[i].SubscriptionResult() <- nil:
|
|
|
|
|
default:
|
|
|
|
|
log.Println("subscription has no listeners") //todo remove*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-05 13:45:53 +00:00
|
|
|
|
}
|
2018-09-07 12:32:26 +00:00
|
|
|
|
w.subsMu2.Unlock()
|
2018-09-05 13:23:03 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 13:17:11 +00:00
|
|
|
|
continue
|
2018-09-05 12:56:32 +00:00
|
|
|
|
}
|
2018-09-06 13:29:49 +00:00
|
|
|
|
//is Event Message
|
|
|
|
|
//there are 2 types of Event Message
|
|
|
|
|
// 1. Publish
|
|
|
|
|
// 2. Delivery
|
|
|
|
|
if transport.IsEventDelivery(msg) {
|
2018-09-07 12:32:26 +00:00
|
|
|
|
w.subsMu2.Lock()
|
|
|
|
|
subscriptions, ok := w.subs2[msg.Channel]
|
2018-09-05 12:56:32 +00:00
|
|
|
|
|
2018-09-06 15:59:56 +00:00
|
|
|
|
if ok {
|
2018-09-07 12:32:26 +00:00
|
|
|
|
//send to all listeners
|
|
|
|
|
for i := range subscriptions {
|
|
|
|
|
if subscriptions[i].MsgChannel() != nil {
|
|
|
|
|
select {
|
|
|
|
|
case subscriptions[i].MsgChannel() <- msg:
|
|
|
|
|
default:
|
|
|
|
|
log.Println("subscription has no listeners") //todo remove
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-06 15:59:56 +00:00
|
|
|
|
}
|
2018-09-06 13:29:49 +00:00
|
|
|
|
}
|
2018-09-07 12:32:26 +00:00
|
|
|
|
w.subsMu2.Unlock()
|
2018-09-06 15:59:56 +00:00
|
|
|
|
|
2018-09-06 13:29:49 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if transport.IsEventPublish(msg) {
|
|
|
|
|
w.onPubResponseMu.Lock()
|
|
|
|
|
onPublish, ok := w.onPublishResponse[msg.Channel]
|
|
|
|
|
w.onPubResponseMu.Unlock()
|
|
|
|
|
if ok {
|
|
|
|
|
onPublish(msg)
|
|
|
|
|
}
|
2018-09-05 13:17:11 +00:00
|
|
|
|
}
|
2018-09-06 13:29:49 +00:00
|
|
|
|
|
2018-09-05 12:56:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Name returns the transport name (websocket)
|
2018-09-04 22:55:52 +00:00
|
|
|
|
func (w *Websocket) Name() string {
|
2018-09-04 23:22:43 +00:00
|
|
|
|
return transportName
|
2018-09-04 22:55:52 +00:00
|
|
|
|
}
|
2018-09-05 12:56:32 +00:00
|
|
|
|
|
|
|
|
|
func (w *Websocket) sendMessage(m *message.Message) error {
|
2018-09-06 15:59:56 +00:00
|
|
|
|
w.connMu.Lock()
|
|
|
|
|
defer w.connMu.Unlock()
|
2018-09-11 10:09:30 +00:00
|
|
|
|
w.topts.Extensions.ApplyOutExtensions(m)
|
2018-09-05 12:56:32 +00:00
|
|
|
|
var payload []message.Message
|
|
|
|
|
payload = append(payload, *m)
|
2018-09-07 16:31:52 +00:00
|
|
|
|
|
|
|
|
|
again:
|
|
|
|
|
err := w.conn.WriteJSON(payload)
|
|
|
|
|
if websocket.IsUnexpectedCloseError(err) {
|
|
|
|
|
advise := w.advice.Load().(*message.Advise)
|
|
|
|
|
if advise.Reconnect == message.ReconnectNone {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//reconnect
|
|
|
|
|
//we should re-register again subscriptions again
|
|
|
|
|
goto again
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-09-05 12:56:32 +00:00
|
|
|
|
}
|
2018-09-04 22:55:52 +00:00
|
|
|
|
func (w *Websocket) nextMsgID() string {
|
|
|
|
|
return strconv.Itoa(int(atomic.AddUint64(w.msgID, 1)))
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Options return the transport Options
|
2018-09-04 22:55:52 +00:00
|
|
|
|
func (w *Websocket) Options() *transport.Options {
|
2018-09-11 10:09:30 +00:00
|
|
|
|
return w.topts
|
2018-09-04 22:55:52 +00:00
|
|
|
|
}
|
2018-09-05 12:56:32 +00:00
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Handshake initiates a connection negotiation by sending a message to the /meta/handshake channel.
|
2018-09-06 09:16:36 +00:00
|
|
|
|
func (w *Websocket) Handshake() (err error) {
|
2018-09-05 12:56:32 +00:00
|
|
|
|
m := message.Message{
|
2018-09-06 12:28:54 +00:00
|
|
|
|
Channel: transport.MetaHandshake,
|
2018-09-04 22:55:52 +00:00
|
|
|
|
Version: "1.0", //todo const
|
2018-09-04 23:22:43 +00:00
|
|
|
|
SupportedConnectionTypes: []string{transportName},
|
2018-09-05 09:28:55 +00:00
|
|
|
|
}
|
2018-09-05 12:56:32 +00:00
|
|
|
|
err = w.sendMessage(&m)
|
|
|
|
|
if err != nil {
|
2018-09-04 22:55:52 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hsResps []message.Message
|
2018-09-04 23:56:25 +00:00
|
|
|
|
if err = w.conn.ReadJSON(&hsResps); err != nil {
|
2018-09-04 22:55:52 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 10:27:19 +00:00
|
|
|
|
resp := &hsResps[0]
|
2018-09-11 10:09:30 +00:00
|
|
|
|
w.topts.Extensions.ApplyInExtensions(resp)
|
2018-09-04 22:55:52 +00:00
|
|
|
|
if resp.GetError() != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
w.clientID = resp.ClientId
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Connect is called after a client has discovered the server’s capabilities with a handshake exchange,
|
|
|
|
|
//a connection is established by sending a message to the /meta/connect channel
|
2018-09-04 22:55:52 +00:00
|
|
|
|
func (w *Websocket) Connect() error {
|
2018-09-05 12:56:32 +00:00
|
|
|
|
m := message.Message{
|
2018-09-06 12:28:54 +00:00
|
|
|
|
Channel: transport.MetaConnect,
|
2018-09-04 23:22:43 +00:00
|
|
|
|
ClientId: w.clientID,
|
|
|
|
|
ConnectionType: transportName,
|
|
|
|
|
Id: w.nextMsgID(),
|
2018-09-05 09:28:55 +00:00
|
|
|
|
}
|
2018-09-05 13:06:25 +00:00
|
|
|
|
go w.readWorker()
|
2018-09-05 12:56:32 +00:00
|
|
|
|
return w.sendMessage(&m)
|
2018-09-04 22:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
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 client object will result in undefined behaviour.
|
2018-09-06 12:28:54 +00:00
|
|
|
|
func (w *Websocket) Disconnect() error {
|
|
|
|
|
m := message.Message{
|
|
|
|
|
Channel: transport.MetaDisconnect,
|
|
|
|
|
ClientId: w.clientID,
|
|
|
|
|
Id: w.nextMsgID(),
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 13:29:49 +00:00
|
|
|
|
w.stopCh <- nil
|
2018-09-06 12:28:54 +00:00
|
|
|
|
close(w.stopCh)
|
2018-09-07 12:32:26 +00:00
|
|
|
|
w.subsMu2.Lock()
|
|
|
|
|
for i := range w.subs2 {
|
|
|
|
|
//close all listeners
|
|
|
|
|
for j := range w.subs2[i] {
|
|
|
|
|
close(w.subs2[i][j].MsgChannel())
|
|
|
|
|
}
|
|
|
|
|
delete(w.subs2, i)
|
2018-09-06 13:37:42 +00:00
|
|
|
|
}
|
2018-09-07 12:32:26 +00:00
|
|
|
|
w.subsMu2.Unlock()
|
2018-09-06 12:28:54 +00:00
|
|
|
|
|
|
|
|
|
return w.sendMessage(&m)
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 12:32:26 +00:00
|
|
|
|
//Subscribe informs the server that messages published to that channel are delivered to itself.
|
|
|
|
|
func (w *Websocket) Subscribe(channel string) (*subscription.Subscription, error) {
|
|
|
|
|
id := w.nextMsgID()
|
|
|
|
|
m := &message.Message{
|
|
|
|
|
Channel: transport.MetaSubscribe,
|
|
|
|
|
ClientId: w.clientID,
|
|
|
|
|
Subscription: channel,
|
|
|
|
|
Id: id,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := w.sendMessage(m); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inMsgCh := make(chan *message.Message, 0)
|
|
|
|
|
subRes := make(chan error)
|
|
|
|
|
|
2018-09-07 13:00:35 +00:00
|
|
|
|
var pub = func(data message.Data) (string, error) {
|
|
|
|
|
return w.Publish(channel, data)
|
|
|
|
|
}
|
|
|
|
|
sub := subscription.NewSubscription(id, channel, w.Unsubscribe, pub, inMsgCh, subRes)
|
2018-09-07 12:32:26 +00:00
|
|
|
|
|
|
|
|
|
w.subsMu2.Lock()
|
|
|
|
|
w.subs2[channel] = append(w.subs2[channel], sub)
|
|
|
|
|
w.subsMu2.Unlock()
|
|
|
|
|
|
|
|
|
|
//todo timeout here
|
|
|
|
|
err := <-subRes
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
log.Println(sub)
|
|
|
|
|
return sub, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Unsubscribe informs the server that the client will no longer listen to incoming event messages on
|
|
|
|
|
//the specified channel/subscription
|
2018-09-07 12:32:26 +00:00
|
|
|
|
func (w *Websocket) Unsubscribe(subscription *subscription.Subscription) error {
|
2018-09-06 10:27:19 +00:00
|
|
|
|
//https://docs.cometd.org/current/reference/#_bayeux_meta_unsubscribe
|
2018-09-07 12:32:26 +00:00
|
|
|
|
w.subsMu2.Lock()
|
|
|
|
|
defer w.subsMu2.Unlock()
|
|
|
|
|
subs, ok := w.subs2[subscription.Channel()]
|
2018-09-06 13:37:42 +00:00
|
|
|
|
if ok {
|
2018-09-07 12:32:26 +00:00
|
|
|
|
var si = -1
|
|
|
|
|
for i := range subs {
|
|
|
|
|
if subs[i] == subscription {
|
|
|
|
|
close(subs[i].MsgChannel())
|
|
|
|
|
si = i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if si > -1 {
|
|
|
|
|
//remove the subscription
|
|
|
|
|
subs = subs[:si+copy(subs[si:], subs[si+1:])]
|
|
|
|
|
}
|
|
|
|
|
w.subs2[subscription.Channel()] = subs
|
|
|
|
|
//if no more listeners to this subscription send unsubscribe to server
|
|
|
|
|
if len(subs) == 0 {
|
|
|
|
|
delete(w.subs2, subscription.Channel())
|
|
|
|
|
//remove onPublishResponse handler
|
|
|
|
|
w.onPubResponseMu.Lock()
|
|
|
|
|
delete(w.onPublishResponse, subscription.Channel())
|
|
|
|
|
w.onPubResponseMu.Unlock()
|
|
|
|
|
m := &message.Message{
|
|
|
|
|
Channel: transport.MetaUnsubscribe,
|
|
|
|
|
Subscription: subscription.Channel(),
|
|
|
|
|
ClientId: w.clientID,
|
|
|
|
|
Id: w.nextMsgID(),
|
|
|
|
|
}
|
|
|
|
|
return w.sendMessage(m)
|
|
|
|
|
}
|
2018-09-06 13:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 12:32:26 +00:00
|
|
|
|
return nil
|
2018-09-04 22:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//Publish publishes events on a channel by sending event messages, the server MAY respond to a publish event
|
|
|
|
|
//if this feature is supported by the server use the OnPublishResponse to get the publish status.
|
2018-09-06 13:29:49 +00:00
|
|
|
|
func (w *Websocket) Publish(subscription string, data message.Data) (id string, err error) {
|
|
|
|
|
id = w.nextMsgID()
|
2018-09-06 11:23:53 +00:00
|
|
|
|
m := &message.Message{
|
|
|
|
|
Channel: subscription,
|
|
|
|
|
Data: data,
|
|
|
|
|
ClientId: w.clientID,
|
2018-09-06 13:29:49 +00:00
|
|
|
|
Id: id,
|
2018-09-06 11:23:53 +00:00
|
|
|
|
}
|
2018-09-06 13:29:49 +00:00
|
|
|
|
if err = w.sendMessage(m); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:31:05 +00:00
|
|
|
|
//OnPublishResponse sets the handler to be triggered if the server replies to the publish request
|
|
|
|
|
//according to the spec the server MAY reply to the publish request, so its not guaranteed that this handler will
|
|
|
|
|
//ever be triggered
|
|
|
|
|
//can be used to identify the status of the published request and for example retry failed published requests
|
2018-09-06 13:29:49 +00:00
|
|
|
|
func (w *Websocket) OnPublishResponse(subscription string, onMsg func(message *message.Message)) {
|
|
|
|
|
w.onPubResponseMu.Lock()
|
|
|
|
|
w.onPublishResponse[subscription] = onMsg
|
|
|
|
|
w.onPubResponseMu.Unlock()
|
2018-09-04 22:55:52 +00:00
|
|
|
|
}
|
2018-09-06 10:27:19 +00:00
|
|
|
|
|
2018-09-06 11:23:53 +00:00
|
|
|
|
func (w *Websocket) handleAdvise(m *message.Advise) {
|
|
|
|
|
//todo actually handle the advice
|
|
|
|
|
w.advice.Store(m)
|
|
|
|
|
}
|