Skip to content

Commit 55ee1e2

Browse files
committed
handler for private links
1 parent e676efe commit 55ee1e2

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

v2/emitter.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ func (c *Client) GenerateKey(key, channel, permissions string, ttl int) (string,
328328
}
329329

330330
// CreatePrivateLink sends a request to create a private link.
331-
func (c *Client) CreatePrivateLink(key, channel, name string, subscribe bool, options ...Option) (*Link, error) {
331+
func (c *Client) CreatePrivateLink(key, channel, name string, optionalHandler MessageHandler, options ...Option) (*Link, error) {
332332
resp, err := c.request("link", &linkRequest{
333333
Name: name,
334334
Key: key,
335335
Channel: formatTopic("", channel, options),
336-
Subscribe: subscribe,
336+
Subscribe: optionalHandler != nil,
337337
Private: true,
338338
})
339339
if err != nil {
@@ -342,18 +342,23 @@ func (c *Client) CreatePrivateLink(key, channel, name string, subscribe bool, op
342342

343343
// Cast the response and return it
344344
if result, ok := resp.(*Link); ok {
345+
if optionalHandler != nil {
346+
c.handlers.AddHandler(result.Channel, optionalHandler)
347+
}
348+
345349
return result, nil
346350
}
351+
347352
return nil, ErrUnmarshal
348353
}
349354

350355
// CreateLink sends a request to create a default link.
351-
func (c *Client) CreateLink(key, channel, name string, subscribe bool, options ...Option) (*Link, error) {
356+
func (c *Client) CreateLink(key, channel, name string, optionalHandler MessageHandler, options ...Option) (*Link, error) {
352357
resp, err := c.request("link", &linkRequest{
353358
Name: name,
354359
Key: key,
355360
Channel: formatTopic("", channel, options),
356-
Subscribe: subscribe,
361+
Subscribe: optionalHandler != nil,
357362
Private: false,
358363
})
359364

@@ -363,6 +368,10 @@ func (c *Client) CreateLink(key, channel, name string, subscribe bool, options .
363368

364369
// Cast the response and return it
365370
if result, ok := resp.(*Link); ok {
371+
if optionalHandler != nil {
372+
c.handlers.AddHandler(result.Channel, optionalHandler)
373+
}
374+
366375
return result, nil
367376
}
368377
return nil, ErrUnmarshal

v2/emitter_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ func clientB(t *testing.T) {
6363

6464
// Ask to create a private link
6565
fmt.Println("[emitter] <- [B] creating a private link")
66-
link, err := c.CreatePrivateLink(key, "sdk-integration-test/", "1", true)
66+
link, err := c.CreatePrivateLink(key, "sdk-integration-test/", "1", func(_ *Client, msg Message) {
67+
fmt.Printf("[emitter] -> [B] received from private link: '%s' topic: '%s'\n", msg.Payload(), msg.Topic())
68+
})
6769
assert.NoError(t, err)
6870
assert.NotNil(t, link)
6971

v2/sample/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func clientB() {
6060

6161
// Ask to create a private link
6262
fmt.Println("[emitter] <- [B] creating a private link")
63-
link, _ := c.CreatePrivateLink(key, "sdk-integration-test/", "1", true)
63+
link, _ := c.CreatePrivateLink(key, "sdk-integration-test/", "1", func(_ *emitter.Client, msg emitter.Message) {
64+
fmt.Printf("[emitter] -> [B] received from private link: '%s' topic: '%s'\n", msg.Payload(), msg.Topic())
65+
})
6466
fmt.Println("[emitter] -> [B] received link " + link.Channel)
6567

6668
// Publish to the private link

0 commit comments

Comments
 (0)