-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from xconnio/client
add WAMP client/session implementation
- Loading branch information
Showing
9 changed files
with
619 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package xconn | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/xconnio/wampproto-go/auth" | ||
) | ||
|
||
type Client struct { | ||
Authenticator auth.ClientAuthenticator | ||
SerializerSpec WSSerializerSpec | ||
|
||
DialTimeout time.Duration | ||
} | ||
|
||
func (c *Client) Connect(ctx context.Context, url string, realm string) (*Session, error) { | ||
if c.SerializerSpec == nil { | ||
c.SerializerSpec = JSONSerializerSpec | ||
} | ||
|
||
joiner := &WebSocketJoiner{ | ||
Authenticator: c.Authenticator, | ||
SerializerSpec: c.SerializerSpec, | ||
DialTimeout: c.DialTimeout, | ||
} | ||
|
||
base, err := joiner.Join(ctx, url, realm) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewSession(base, c.SerializerSpec.Serializer()), nil // nolint: contextcheck | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package xconn_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/xconnio/xconn-go" | ||
) | ||
|
||
func connect(t *testing.T) *xconn.Session { | ||
listener := startRouter(t, "realm1") | ||
defer func() { _ = listener.Close() }() | ||
address := fmt.Sprintf("ws://%s/ws", listener.Addr().String()) | ||
|
||
client := &xconn.Client{} | ||
|
||
session, err := client.Connect(context.Background(), address, "realm1") | ||
require.NoError(t, err) | ||
require.NotNil(t, session) | ||
|
||
return session | ||
} | ||
|
||
func TestCall(t *testing.T) { | ||
session := connect(t) | ||
t.Run("CallNoProc", func(t *testing.T) { | ||
result, err := session.Call(context.Background(), "foo.bar", nil, nil, nil) | ||
require.Error(t, err) | ||
require.Nil(t, result) | ||
|
||
var er *xconn.Error | ||
errors.As(err, &er) | ||
require.Equal(t, "wamp.error.no_such_procedure", er.URI) | ||
}) | ||
} | ||
|
||
func TestRegisterCall(t *testing.T) { | ||
session := connect(t) | ||
reg, err := session.Register( | ||
context.Background(), | ||
"foo.bar", | ||
func(ctx context.Context, invocation *xconn.Invocation) (*xconn.Result, error) { | ||
return &xconn.Result{Args: []any{"hello"}}, nil | ||
}, | ||
nil, | ||
) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, reg) | ||
|
||
t.Run("Call", func(t *testing.T) { | ||
result, err := session.Call(context.Background(), "foo.bar", nil, nil, nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, result) | ||
|
||
require.Equal(t, "hello", result.Args[0]) | ||
}) | ||
} | ||
|
||
func TestPublishSubscribe(t *testing.T) { | ||
session := connect(t) | ||
event1 := make(chan *xconn.Event, 1) | ||
reg, err := session.Subscribe( | ||
context.Background(), | ||
"foo.bar", | ||
func(event *xconn.Event) { | ||
event1 <- event | ||
}, | ||
nil, | ||
) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, reg) | ||
|
||
t.Run("Publish", func(t *testing.T) { | ||
opt := map[string]any{ | ||
"exclude_me": false, | ||
} | ||
err := session.Publish(context.Background(), "foo.bar", nil, nil, opt) | ||
require.NoError(t, err) | ||
|
||
event := <-event1 | ||
log.Println(event) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.