Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I wanna Add addition params [topicFilter] on Event of client.on("messsage",callback) #1846

Open
LZW-Andrewlu opened this issue Apr 19, 2024 · 2 comments

Comments

@LZW-Andrewlu
Copy link

Is your feature request related to a problem? Please describe.
When I subscribe to a topic with wildcard characters, I want to distinguish which topicFilter it is in the message event and distribute it to the corresponding callback function, and the real topic field in the message event needs a matching process with the wildcard theme.

Describe the solution you'd like
Option 1: I hope the client can provide an API to detect whether a wildcard topic matches the actual topic in the message event callback;
Option 2: Add a parameter to onMessageCallback to indicate which subscription topic matches the current topic. This way, I can execute different event callbacks for different subscription topics in the message event,thus not match them manually with if...else...

Describe alternatives you've considered

Additional context
i want sub function like code below. and each callback only work with absolute topic&topicFilter

        sub(topic: string, callback: OnMessageCallback) {
            if (this.inst) {
                this.inst.subscribe(topic);
            }
            this.subTopics[topic] = new WeakRef(callback);
        },
@LZW-Andrewlu
Copy link
Author

Try to implement this function as below:

public static isMatched(topicFilter: string, topicName: string): boolean {
        let topicPos = 0;
        let filterPos = 0;
        let topicLen = topicName.length;
        let filterLen = topicFilter.length;
        if (topicFilter === topicName) {
            return true;
        } else {
            while (filterPos < filterLen && topicPos < topicLen) {
                if (topicFilter.charAt(filterPos) == "#") {
                    topicPos = topicLen;
                    filterPos = filterLen;
                    break;
                }
                if (
                    (topicName.charAt(topicPos) == "/" && topicFilter.charAt(filterPos) != "/") ||
                    (topicFilter.charAt(filterPos) != "+" &&
                        topicFilter.charAt(filterPos) != "#" &&
                        topicFilter.charAt(filterPos) != topicName.charAt(topicPos))
                ) {
                    break;
                }

                if (topicFilter.charAt(filterPos) == "+") {
                    for (let nextpos = topicPos + 1; nextpos < topicLen && topicName.charAt(nextpos) != "/"; nextpos = topicPos + 1) {
                        ++topicPos;
                    }
                }
                ++filterPos;
                ++topicPos;
            }
            if (topicPos == topicLen && filterPos == filterLen) {
                return true;
            } else {
                if (topicFilter.length - filterPos > 0 && topicPos == topicLen) {
                    if (topicName.charAt(topicPos - 1) == "/" && topicFilter.charAt(filterPos) == "#") {
                        return true;
                    }
                    if (topicFilter.length - filterPos > 1 && topicFilter.substring(filterPos, filterPos + 2) == "/#") {
                        return true;
                    }
                }
                return false;
            }
        }
    }

@robertsLando
Copy link
Member

Feel free to submit a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants