Last active
February 12, 2018 23:18
-
-
Save apcera-code/3672bf7bd20012823797849096ed1a4d to your computer and use it in GitHub Desktop.
Sample WAMP client written in Go using an open-source WAMP Go library
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
package main | |
import ( | |
"fmt" | |
turnpike "gopkg.in/jcelliott/turnpike.v2" | |
) | |
// Events realm for WAMP protocol specified by Apcera Platform. | |
const ( | |
eventsRealm = "com.apcera.api.es" | |
) | |
// SubscribeToEvents subscribes to events in Apcera Platform running | |
// under given clusterDomain using authorization token provided and | |
// for given subject (An FQN) specifying a resource or group of resources. | |
func SubscribeToEvents(clusterDomain string, authToken string, subject string) { | |
// Access URL with token in query parameters. | |
accessURL := fmt.Sprintf("wss://%s/v1/wamp?authorization=%s", clusterDomain, authToken) | |
// Note: In production TLS is recommended. | |
wampClient, err := turnpike.NewWebsocketClient(turnpike.JSON, accessURL, nil) | |
if err != nil { | |
fmt.Printf("Error connecting to WAMP router at %s: %s", clusterDomain, err) | |
} | |
// Join Events realm specified by Apcera Platform. | |
_, err = wampClient.JoinRealm(eventsRealm, nil) | |
if err != nil { | |
fmt.Printf("Error joining events realm at: %s", clusterDomain, err) | |
} | |
// Callback function to receive events. | |
handleEventStream := func(args interface{}, kwargs map[string]interface{}) { | |
event := args[0].(map[string]interface{}) | |
// Construct JSON from the received event data. | |
jsonEvent, err := json.Marshal(event) | |
if err != nil { | |
fmt.Printf("error converting event %v to JSON format: %s", event, err) | |
} | |
fmt.Println(jsonEvent) | |
} | |
// Subscribe to events on the subject. | |
if err := wampClient.Subscribe(subject, handleEventStream); err != nil { | |
fmt.Printf("Error subscribing to events for %s at %s: %s", subject, clusterDomain, err) | |
} | |
// If necessary use a timer to track that subscription is receiving events | |
// continuously (by resetting the timer in handleEventStream) and if not timeout. | |
for { | |
select { | |
case <-wampClient.ReceiveDone: | |
fmt.Println("Events receiving done, exiting!") | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
severity: 'Error'
message: 'not enough arguments in call to turnpike.NewWebsocketClient
have (turnpike.Serialization, string, nil)
want (turnpike.Serialization, string, *tls.Config, turnpike.DialFunc)'