fayec/transport/websocket/websocket.go

171 lines
3.5 KiB
Go
Raw Normal View History

2018-09-04 22:55:52 +00:00
package websocket
import (
2018-09-05 09:28:55 +00:00
"encoding/json"
2018-09-04 22:55:52 +00:00
"github.com/gorilla/websocket"
"github.com/thesyncim/faye/message"
"github.com/thesyncim/faye/transport"
2018-09-05 09:28:55 +00:00
"log"
2018-09-04 22:55:52 +00:00
"strconv"
2018-09-05 09:59:20 +00:00
"strings"
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-05 09:28:55 +00:00
var Debug = true
func debugJson(v interface{}) string {
b, _ := json.MarshalIndent(v, "", " ")
return string(b)
}
2018-09-04 22:55:52 +00:00
type Websocket struct {
TransportOpts *transport.Options
conn *websocket.Conn
clientID string
msgID *uint64
2018-09-05 12:56:32 +00:00
subsMu sync.Mutex //todo sync.Map
subs map[string]chan *message.Message
2018-09-04 22:55:52 +00:00
}
var _ transport.Transport = (*Websocket)(nil)
func (w *Websocket) Init(options *transport.Options) error {
var (
err error
msgID uint64
)
w.TransportOpts = options
w.msgID = &msgID
2018-09-05 12:56:32 +00:00
w.subs = map[string]chan *message.Message{}
2018-09-04 22:55:52 +00:00
w.conn, _, err = websocket.DefaultDialer.Dial(options.Url, nil)
if err != nil {
return err
}
return nil
}
2018-09-05 12:56:32 +00:00
func (w *Websocket) readWorker() error {
var payload []message.Message
for {
err := w.conn.ReadJSON(&payload)
if err != nil {
return err
}
//dispatch
msg := payload[0]
if strings.HasPrefix(msg.Channel, "/meta") {
continue //todo update introspect message and update state
}
w.subsMu.Lock()
subscription := w.subs[msg.Channel]
w.subsMu.Unlock()
subscription <- &msg
}
}
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 {
var payload []message.Message
payload = append(payload, *m)
if Debug {
log.Println("sending request", debugJson(payload))
}
return w.conn.WriteJSON(payload)
}
2018-09-04 22:55:52 +00:00
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) {
2018-09-05 12:56:32 +00:00
m := message.Message{
2018-09-04 22:55:52 +00:00
Channel: string(transport.Handshake),
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-05 09:28:55 +00:00
if Debug {
log.Println("handshake response", debugJson(hsResps))
}
2018-09-04 22:55:52 +00:00
resp := hsResps[0]
if resp.GetError() != nil {
return err
}
2018-09-05 12:56:32 +00:00
log.Println(debugJson(resp))
2018-09-04 22:55:52 +00:00
w.clientID = resp.ClientId
return nil
}
func (w *Websocket) Connect() error {
2018-09-05 12:56:32 +00:00
m := message.Message{
2018-09-04 23:22:43 +00:00
Channel: string(transport.Connect),
ClientId: w.clientID,
ConnectionType: transportName,
Id: w.nextMsgID(),
2018-09-05 09:28:55 +00:00
}
//todo verify if extensions are applied on connect,verify if hs is complete
2018-09-05 12:56:32 +00:00
return w.sendMessage(&m)
2018-09-04 22:55:52 +00:00
}
func (w *Websocket) Subscribe(subscription string, onMessage func(message *message.Message)) error {
2018-09-04 23:56:25 +00:00
m := &message.Message{
Channel: string(transport.Subscribe),
ClientId: w.clientID,
2018-09-05 09:28:55 +00:00
Subscription: subscription,
2018-09-04 23:56:25 +00:00
Id: w.nextMsgID(),
}
if w.TransportOpts.OutExt != nil {
w.TransportOpts.OutExt(m)
}
2018-09-05 08:56:45 +00:00
2018-09-05 12:56:32 +00:00
if err := w.sendMessage(m); err != nil {
2018-09-04 23:56:25 +00:00
return err
}
2018-09-05 12:56:32 +00:00
//todo validate
2018-09-04 23:56:25 +00:00
2018-09-05 12:56:32 +00:00
inMsgCh := make(chan *message.Message, 0)
2018-09-05 08:56:45 +00:00
2018-09-05 12:56:32 +00:00
w.subs[subscription] = inMsgCh
2018-09-04 23:56:25 +00:00
2018-09-05 12:56:32 +00:00
var inMsg *message.Message
for inMsg = range inMsgCh {
onMessage(inMsg)
2018-09-04 23:56:25 +00:00
}
return nil
2018-09-04 22:55:52 +00:00
}
func (w *Websocket) Unsubscribe(subscription string) error {
panic("not implemented")
}
func (w *Websocket) Publish(subscription string, message *message.Message) error {
panic("not implemented")
}