Working authenticated connection
This commit is contained in:
parent
11b176dcbc
commit
c89756630f
10
client.go
10
client.go
@ -1,11 +1,11 @@
|
|||||||
package fayec
|
package fayec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/thesyncim/faye/internal/dispatcher"
|
"github.com/thesyncim/fayec/internal/dispatcher"
|
||||||
"github.com/thesyncim/faye/message"
|
"github.com/thesyncim/fayec/message"
|
||||||
"github.com/thesyncim/faye/subscription"
|
"github.com/thesyncim/fayec/subscription"
|
||||||
"github.com/thesyncim/faye/transport"
|
"github.com/thesyncim/fayec/transport"
|
||||||
_ "github.com/thesyncim/faye/transport/websocket"
|
_ "github.com/thesyncim/fayec/transport/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
|
42
examples/groupme.go
Normal file
42
examples/groupme.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/thesyncim/fayec"
|
||||||
|
"github.com/thesyncim/fayec/message"
|
||||||
|
"github.com/thesyncim/fayec/subscription"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var authorizationToken = "ABC"
|
||||||
|
|
||||||
|
var authenticationExtension message.Extension = func(message *message.Message) {
|
||||||
|
if message.Channel == "/meta/subscribe" {
|
||||||
|
message.Ext = map[string]string{
|
||||||
|
"access_token": authorizationToken,
|
||||||
|
"timestamp": string(time.Now().Unix()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
client, err := fayec.NewClient("wss://push.groupme.com/faye", fayec.WithOutExtension(authenticationExtension))
|
||||||
|
defer client.Disconnect()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Errorf("Error connecting to groupme", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var sub *subscription.Subscription
|
||||||
|
sub, err = client.Subscribe("/user/13685836")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = sub.OnMessage(func(channel string, data message.Data) {
|
||||||
|
fmt.Println(data)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
8
go.mod
Normal file
8
go.mod
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module github.com/thesyncim/fayec
|
||||||
|
|
||||||
|
go 1.21.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gorilla/websocket v1.5.0
|
||||||
|
github.com/pkg/errors v0.9.1
|
||||||
|
)
|
6
go.sum
Normal file
6
go.sum
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/thesyncim/faye v0.0.0-20180924151438-11b176dcbcb9 h1:Pz1lvYNPL7e4krAyn30sMi8bWJKHH1wbl7VMBmIybws=
|
||||||
|
github.com/thesyncim/faye v0.0.0-20180924151438-11b176dcbcb9/go.mod h1:lYnF5uSFlOcMkYn8aWvtnD1Qx59LxZW2sIQgiDXNoUc=
|
@ -2,10 +2,10 @@ package dispatcher
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/thesyncim/faye/internal/store"
|
"github.com/thesyncim/fayec/internal/store"
|
||||||
"github.com/thesyncim/faye/message"
|
"github.com/thesyncim/fayec/message"
|
||||||
"github.com/thesyncim/faye/subscription"
|
"github.com/thesyncim/fayec/subscription"
|
||||||
"github.com/thesyncim/faye/transport"
|
"github.com/thesyncim/fayec/transport"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
@ -180,6 +180,7 @@ func (d *Dispatcher) Subscribe(channel string) (*subscription.Subscription, erro
|
|||||||
Subscription: channel,
|
Subscription: channel,
|
||||||
Id: id,
|
Id: id,
|
||||||
}
|
}
|
||||||
|
d.extensions.ApplyOutExtensions(m)
|
||||||
|
|
||||||
if err := d.transport.SendMessage(m); err != nil {
|
if err := d.transport.SendMessage(m); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/thesyncim/faye/subscription"
|
"github.com/thesyncim/fayec/subscription"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -7,8 +7,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
wildcardSubscription, _ = subscription.NewSubscription("a", "/wildcard/*", nil, nil, nil)
|
wildcardSubscription, _ = subscription.NewSubscription("a", nil, nil)
|
||||||
simpleSubscription, _ = subscription.NewSubscription("b", "/foo/bar", nil, nil, nil)
|
simpleSubscription, _ = subscription.NewSubscription("b", nil, nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStore_Add(t *testing.T) {
|
func TestStore_Add(t *testing.T) {
|
||||||
|
@ -2,7 +2,7 @@ package subscription
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/thesyncim/faye/message"
|
"github.com/thesyncim/fayec/message"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ package transport
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"github.com/thesyncim/faye/message"
|
"github.com/thesyncim/fayec/message"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/thesyncim/faye/message"
|
"github.com/thesyncim/fayec/message"
|
||||||
"github.com/thesyncim/faye/transport"
|
"github.com/thesyncim/fayec/transport"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
@ -43,13 +46,11 @@ func (w *Websocket) Init(endpoint string, options *transport.Options) error {
|
|||||||
|
|
||||||
w.stopCh = make(chan error)
|
w.stopCh = make(chan error)
|
||||||
w.conn, _, err = websocket.DefaultDialer.Dial(endpoint, options.Headers)
|
w.conn, _, err = websocket.DefaultDialer.Dial(endpoint, options.Headers)
|
||||||
|
err = w.conn.UnderlyingConn().(*tls.Conn).NetConn().(*net.TCPConn).SetKeepAlive(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w.conn.SetPingHandler(func(appData string) error {
|
|
||||||
return w.conn.WriteJSON(make([]struct{}, 0))
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -67,16 +68,22 @@ func (w *Websocket) readWorker() error {
|
|||||||
case err := <-w.stopCh:
|
case err := <-w.stopCh:
|
||||||
return err
|
return err
|
||||||
default:
|
default:
|
||||||
}
|
|
||||||
var payload []message.Message
|
var payload []message.Message
|
||||||
err := w.conn.ReadJSON(&payload)
|
_, data, err := w.conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data, &payload)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
//dispatch
|
//dispatch
|
||||||
msg := &payload[0]
|
msg := &payload[0]
|
||||||
w.onMsg(msg)
|
w.onMsg(msg)
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user