-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
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;
}
}
} |
Feel free to submit a PR |
I dont think this matcher functionallity should be implemented in this library because it would get bloated if to many use case specific logic is packed in here. But feel free to create a facade on this library for your needs. As inspiration you can checkout my facade in my monorepo |
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
The text was updated successfully, but these errors were encountered: