Skip to content

Netcracker/qubership-core-lib-go-stomp-websocket

Go build Coverage duplicated_lines_density vulnerabilities bugs code_smells

go-stomp-websocket

Golang implementation of a STOMP client over WebSocket.

Supported operations:

  • Establishing a STOMP connection
  • Subscribing to events

Usage:

To start using the STOMP client:

  1. Define the connection URL
  2. Create the STOMP client using either a token or a custom Dial
  3. Define a channel to receive frames

Example

To connect to the Watch API of Tenant-Manager, which uses the STOMP protocol and has the following URL:

ws://tenant-manager:8080/api/v3/tenant-manager/watch

Creating the STOMP client

Using a token
token, _ := tenantWatchClient.Credential.GetAuthToken()
url, _ := url.Parse("ws://localhost:8080/api/v3/tenant-manager/watch")
stompClient, _ := go_stomp_websocket.ConnectWithToken(*url, token)
Using a custom Dial
type ConnectionDialer interface {
    Dial(webSocketURL url.URL, dialer websocket.Dialer, requestHeaders http.Header) (*websocket.Conn, *http.Response, error)
}
url, _ := url.Parse("ws://localhost:8080/api/v3/tenant-manager/watch")
dialer := websocket.Dialer{}
// configure the dialer
requestHeaders := http.Header{}
// add headers
connDial := ConnectionDialerImpl{} // implements the Dial method of ConnectionDialer interface
stompClient, _ := go_stomp_websocket.Connect(*url, dialer, requestHeaders, connDial)

Subscribe to events:

subscr, _ := stompClient.Subscribe("/tenant-changed")

Handle received frames:

go func() {
    for {
        var tenant = new(tenant.Tenant)
        frame := <-subscr.FrameCh // Receive frame
        if len(frame.Body) > 0 {
            err := json.Unmarshal([]byte(frame.Body), tenant) // Parse the frame body into Tenant structure
            if err != nil {
                fmt.Println(err)
            } else {
                fmt.Printf("Received tenant with id:%s\n", tenant.ObjectId)
            }
        }
    }
}()

About

No description or website provided.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 8

Languages