NaikSoftware / StompProtocolAndroid

STOMP protocol via WebSocket for Android
MIT License
597 stars 265 forks source link

Matching between MESSAGE and subscription should use the subscription header #171

Closed laurentpeters closed 4 years ago

laurentpeters commented 4 years ago

According to the Stomp specification,

Message

The MESSAGE frame MUST include a destination header indicating the destination the message was sent to. If the message has been sent using STOMP, this destination header SHOULD be identical to the one used in the corresponding SEND frame.

The MESSAGE frame MUST also contain a message-id header with a unique identifier for that message and a subscription header matching the identifier of the subscription that is receiving the message.

The matching between the subscription and the message should be done using the subscription header. There is no guarantee that the destination header is identical to the one used in the Subscribe.

In the library, the SimplePathMatcher matches the destination header.

Another PathMatcher could be defined that uses the subscription header. Unfortunately, the subscription id is not available outside the StompClient class. Adding the following method to the StompClient would solve this issue:

    public String getTopicId(String dest) {
        return topics.get(dest);
    }

A PathMatcher could then be defined using this method:

           @Override
            public boolean matches(String path, StompMessage msg) {
               // Compare subscription
                String pathSubscription = stompClient.getTopicId(path);
                if (pathSubscription == null) return false;
                String subscription = msg.findHeader(StompHeader.SUBSCRIPTION);
                return pathSubscription.equals(subscription);
            }