Working authenticated connection

This commit is contained in:
Brandon Watson 2023-09-18 20:58:15 -05:00
parent 11b176dcbc
commit c89756630f
10 changed files with 126 additions and 62 deletions

View File

@ -1,11 +1,11 @@
package fayec
import (
"github.com/thesyncim/faye/internal/dispatcher"
"github.com/thesyncim/faye/message"
"github.com/thesyncim/faye/subscription"
"github.com/thesyncim/faye/transport"
_ "github.com/thesyncim/faye/transport/websocket"
"github.com/thesyncim/fayec/internal/dispatcher"
"github.com/thesyncim/fayec/message"
"github.com/thesyncim/fayec/subscription"
"github.com/thesyncim/fayec/transport"
_ "github.com/thesyncim/fayec/transport/websocket"
)
type options struct {

42
examples/groupme.go Normal file
View 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
View 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
View 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=

View File

@ -2,10 +2,10 @@ package dispatcher
import (
"fmt"
"github.com/thesyncim/faye/internal/store"
"github.com/thesyncim/faye/message"
"github.com/thesyncim/faye/subscription"
"github.com/thesyncim/faye/transport"
"github.com/thesyncim/fayec/internal/store"
"github.com/thesyncim/fayec/message"
"github.com/thesyncim/fayec/subscription"
"github.com/thesyncim/fayec/transport"
"log"
"strconv"
"sync"
@ -180,6 +180,7 @@ func (d *Dispatcher) Subscribe(channel string) (*subscription.Subscription, erro
Subscription: channel,
Id: id,
}
d.extensions.ApplyOutExtensions(m)
if err := d.transport.SendMessage(m); err != nil {
return nil, err

View File

@ -1,7 +1,7 @@
package store
import (
"github.com/thesyncim/faye/subscription"
"github.com/thesyncim/fayec/subscription"
"sync"
)

View File

@ -7,8 +7,8 @@ import (
)
var (
wildcardSubscription, _ = subscription.NewSubscription("a", "/wildcard/*", nil, nil, nil)
simpleSubscription, _ = subscription.NewSubscription("b", "/foo/bar", nil, nil, nil)
wildcardSubscription, _ = subscription.NewSubscription("a", nil, nil)
simpleSubscription, _ = subscription.NewSubscription("b", nil, nil)
)
func TestStore_Add(t *testing.T) {

View File

@ -2,7 +2,7 @@ package subscription
import (
"errors"
"github.com/thesyncim/faye/message"
"github.com/thesyncim/fayec/message"
"regexp"
)

View File

@ -2,7 +2,7 @@ package transport
import (
"crypto/tls"
"github.com/thesyncim/faye/message"
"github.com/thesyncim/fayec/message"
"net/http"
"time"
)

View File

@ -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"
)
@ -43,13 +46,11 @@ 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
}
@ -67,16 +68,22 @@ func (w *Websocket) readWorker() error {
case err := <-w.stopCh:
return err
default:
}
var payload []message.Message
err := w.conn.ReadJSON(&payload)
_, 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)
}
}
}