// Create and configure ConnectParameters using ConnectParametersBuilder.varparameters=newConnectParametersBuilder().WithHost("localhost",15674).WithTransport(SupportedTransports.WebSocket).WithPath("/ws").WithVirtualHost("/").WithCredentials(newCredentials("guest","guest")).Build();// Begin connectionstompClient.BeginConnect(parameters);
Both the connect and disconnect methods of the client support asynchronous operations with their respective ConnectAsync and DisconnectAsync functions.
Now that we can connect to the broker, we can start add subscriptions and send messages. To add a subscription we can use the OnConnected event, because that's the earliest event that we know the client is connected and can send STOMP messages to the broker.
We have to use the STOMP client's CreateSubscriptionBuilder to create and set up a SubscriptionBuilder. The minimum data we have to provide is the subscription destination ("/queue/test" in this case) that we have to pass to the [CreateSubscriptionBuilder](../api-reference/STOMP/Client.md#subscriptionbuilder-createsubscriptionbuilderstring] call. When we finished setting up the builder, call its BeginSubscribe method to actually start the subscription process.
OnSubsriptionAck will be called when the subscription is acknowledged by the broker.
OnTestQueueCallback will be called every time when the broker is sending a message to the client.
After, or even in the OnConnected event the client is free to send messages to the broker. Sending a message is similar to how the subscription is created, a MessageBuilder can be created using the STOMP client's CreateMessageBuilder and passing the message's destination ("/queue/test" in this case).
client.CreateMessageBuilder("/queue/test").WithContent("Hello Text World!").WithHeader("custom-header","custom header value!").WithAcknowledgmentCallback((client,frame)=>Debug.Log("Message Sent & Processed!")).BeginSend();
awaitclient.CreateMessageBuilder("/queue/test").WithContent("Hello Text World!").WithHeader("custom-header","custom header value!").SendAsync();Debug.Log("Message Sent & Processed!");
Most of the calls are optional, but setting the destination, content and calling BeginSend are mandatory.
Of course, this step is different for every application, the STOMP client doesn't have to be bound to a GameObject or let alone to one scene, it can even outlive scene transitions.