add debug option
This commit is contained in:
parent
6eaff2fa34
commit
42dbadb947
@ -1,9 +1,11 @@
|
|||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/thesyncim/faye/message"
|
"github.com/thesyncim/faye/message"
|
||||||
"github.com/thesyncim/faye/transport"
|
"github.com/thesyncim/faye/transport"
|
||||||
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
@ -14,6 +16,13 @@ func init() {
|
|||||||
transport.RegisterTransport(&Websocket{})
|
transport.RegisterTransport(&Websocket{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var Debug = true
|
||||||
|
|
||||||
|
func debugJson(v interface{}) string {
|
||||||
|
b, _ := json.MarshalIndent(v, "", " ")
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
type Websocket struct {
|
type Websocket struct {
|
||||||
TransportOpts *transport.Options
|
TransportOpts *transport.Options
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
@ -50,11 +59,15 @@ func (w *Websocket) Options() *transport.Options {
|
|||||||
}
|
}
|
||||||
func (w *Websocket) Handshake() (err error) {
|
func (w *Websocket) Handshake() (err error) {
|
||||||
var payload []message.Message
|
var payload []message.Message
|
||||||
if err = w.conn.WriteJSON(append(payload, message.Message{
|
payload = append(payload, message.Message{
|
||||||
Channel: string(transport.Handshake),
|
Channel: string(transport.Handshake),
|
||||||
Version: "1.0", //todo const
|
Version: "1.0", //todo const
|
||||||
SupportedConnectionTypes: []string{transportName},
|
SupportedConnectionTypes: []string{transportName},
|
||||||
})); err != nil {
|
})
|
||||||
|
if Debug {
|
||||||
|
log.Println("handshake request", debugJson(payload))
|
||||||
|
}
|
||||||
|
if err = w.conn.WriteJSON(payload); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +75,9 @@ func (w *Websocket) Handshake() (err error) {
|
|||||||
if err = w.conn.ReadJSON(&hsResps); err != nil {
|
if err = w.conn.ReadJSON(&hsResps); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if Debug {
|
||||||
|
log.Println("handshake response", debugJson(hsResps))
|
||||||
|
}
|
||||||
|
|
||||||
resp := hsResps[0]
|
resp := hsResps[0]
|
||||||
if resp.GetError() != nil {
|
if resp.GetError() != nil {
|
||||||
@ -73,20 +89,24 @@ func (w *Websocket) Handshake() (err error) {
|
|||||||
|
|
||||||
func (w *Websocket) Connect() error {
|
func (w *Websocket) Connect() error {
|
||||||
var payload []message.Message
|
var payload []message.Message
|
||||||
//todo verify if extensions are applied on connect,verify if hs is complete
|
payload = append(payload, message.Message{
|
||||||
return w.conn.WriteJSON(append(payload, message.Message{
|
|
||||||
Channel: string(transport.Connect),
|
Channel: string(transport.Connect),
|
||||||
ClientId: w.clientID,
|
ClientId: w.clientID,
|
||||||
ConnectionType: transportName,
|
ConnectionType: transportName,
|
||||||
Id: w.nextMsgID(),
|
Id: w.nextMsgID(),
|
||||||
}))
|
})
|
||||||
|
if Debug {
|
||||||
|
log.Println("connect request", debugJson(payload))
|
||||||
|
}
|
||||||
|
//todo verify if extensions are applied on connect,verify if hs is complete
|
||||||
|
return w.conn.WriteJSON(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Websocket) Subscribe(subscription string, onMessage func(message *message.Message)) error {
|
func (w *Websocket) Subscribe(subscription string, onMessage func(message *message.Message)) error {
|
||||||
m := &message.Message{
|
m := &message.Message{
|
||||||
Channel: string(transport.Subscribe),
|
Channel: string(transport.Subscribe),
|
||||||
ClientId: w.clientID,
|
ClientId: w.clientID,
|
||||||
Subscription: "/" + subscription,
|
Subscription: subscription,
|
||||||
Id: w.nextMsgID(),
|
Id: w.nextMsgID(),
|
||||||
}
|
}
|
||||||
if w.TransportOpts.OutExt != nil {
|
if w.TransportOpts.OutExt != nil {
|
||||||
@ -94,7 +114,11 @@ func (w *Websocket) Subscribe(subscription string, onMessage func(message *messa
|
|||||||
}
|
}
|
||||||
|
|
||||||
var payload []message.Message
|
var payload []message.Message
|
||||||
err := w.conn.WriteJSON(append(payload, *m))
|
payload = append(payload, *m)
|
||||||
|
if Debug {
|
||||||
|
log.Println("subscribe request", debugJson(payload))
|
||||||
|
}
|
||||||
|
err := w.conn.WriteJSON(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -103,6 +127,9 @@ func (w *Websocket) Subscribe(subscription string, onMessage func(message *messa
|
|||||||
if err = w.conn.ReadJSON(&hsResps); err != nil {
|
if err = w.conn.ReadJSON(&hsResps); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if Debug {
|
||||||
|
log.Println("subscribe response", debugJson(hsResps))
|
||||||
|
}
|
||||||
|
|
||||||
subResp := hsResps[0]
|
subResp := hsResps[0]
|
||||||
if subResp.GetError() != nil {
|
if subResp.GetError() != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user