StompClientLib-取消订阅socketclient

Kru*_*nal 2 activemq-classic stomp ios swift swift4

我在项目中添加了StompClientLib,并且在取消订阅目标主题时遇到了问题。

取消订阅目标会出现以下错误:“ org.apache.activemq.transport.stomp.ProtocolException:没有匹配的订阅。\ r \ tat org.apache.activemq.transport.stomp.ProtocolConverter.onStompUnsubscribe(ProtocolConverter.java:734)\ r \ tat org.apache.activemq.transport.stomp.ProtocolConverter.onStompCommand(ProtocolConverter.java:262)\ r \ tat org.apache.activemq.transport.ws.AbstractStompSocket.processStompFrame(AbstractStompSocket.java:151)\ r \ tat org.apache.activemq.transport.ws.jetty9.StompSocket.onWebSocketText(StompSocket.java:96)\ r \ tat org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.onTextMessage(JettyListenerEventDriver.java:128)\ r \ tat org.eclipse.jetty.websocket.common.message.SimpleTextMessage.messageComplete(SimpleTextMessage.java:69)\ r \ tat org.eclipse.jetty.websocket.common.events.AbstractEventDriver。appendMessage(AbstractEventDriver.java:64)\ r \ tat org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.onTextFrame(JettyListenerEventDriver.java:122)\ r \ tat org.eclipse.jetty.websocket.common.events。 AbstractEventDriver.incomingFrame(AbstractEventDriver.java:160)\ r \ tat org.eclipse.jetty.websocket.common.WebSocketSession.incomingFrame(WebSocketSession.java:309)\ r \ tat org.eclipse.jetty.websocket.common.extensions。 ExtensionStack.incomingFrame(ExtensionStack.java:214)\ r \ tat org.eclipse.jetty.websocket.common.Parser.notifyFrame(Parser.java:220)\ r \ tat org.eclipse.jetty.websocket.common.Parser。 parse(Parser.java:258)\ r \ tat org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:628)\ r \ tat org.eclipse.jetty.websocket.common.io。 AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection。java:476)\ r \ tat org.eclipse.jetty.io.AbstractConnection $ 2.run(AbstractConnection.java:540)\ r \ t org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635 )\ r \ tat org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:555)\ r \ tat java.lang.Thread.run(Unknown Source)\ r“)

是的,这是订阅名称的问题,但是不接受我用来订阅特定频道的相同字符串。

例如:

```
// destination
let destinationChannelTopic = "/topic/channel_1234"

// subscribe successful
socketClient?.subscribe(destination: destinationChannelTopic)

// unsubscribe not successful with same destination
socketClient?.unsubscribe(destination: destinationChannelTopic)
```
Run Code Online (Sandbox Code Playgroud)

取消订阅时会出现以下错误:没有匹配的订阅者
有人可以帮我理解,怎么了?我究竟做错了什么?

正如我从“ 订阅和接收消息”中分析的那样,订阅(订阅方法)从服务器返回一个字符串(订阅通道ID),我们需要将其存储在客户端的某个位置(在我们的项目/代码中),并且需要使用相同的字符串要取消订阅的字符串。

这是javcScript(不是iOS-swift)代码,此链接(订阅和接收消息)中提供了一个示例,我们在Web应用程序中实现了类似的方式:

```
// subscribe
var subscription = client.subscribe("/queue/test", callback);
```
Run Code Online (Sandbox Code Playgroud)

subscription()方法返回一个JavaScript对象,该对象具有1个属性ID(对应于客户端订阅ID)和一个方法unsubscribe(),该方法稍后可用于从此目标取消订阅客户端。

```
// unsubscribe
subscription.unsubscribe();
```
Run Code Online (Sandbox Code Playgroud)

因此,这仅是用于订阅和取消订阅的方法/可行方式。如果是,那么我们没有subscribe(...)退订的任何价值。我没有从订阅socketClient?.subscribe(destination: destinationChannelTopic)方法中获得任何价值(目的地订阅ID)

作为此问题的替代解决方案,我将客户端与服务器断开连接,然后再次重新连接+再次订阅所有其他目标。这不是正确的处理方式,但是我目前只有这种解决方案。

请帮助找出解决此问题的方法。

这是有关此问题的参考链接:取消订阅socketclient#14

ind*_*dus 7

用于订阅和取消订阅的订阅方法学方法有很大不同。

订阅使用唯一的目标(id)与服务器作为目标进行连接,但通过订阅ID链接并记住它,并在取消订阅时使用订阅ID在服务器上标识自己。

这是代码,您正在寻找。试试吧。

let destination = "/topic/channel_1234"
let ack = "ack_\(destination)" // It can be any unique string
let subsId = "subscription_\(destination)" // It can be any unique string
let header = ["destination": destination, "ack": ack, "id": subsId]

// subscribe
socketClient?.subscribeWithHeader(destination: destination, withHeader: header)

// unsubscribe
socketClient?.unsubscribe(destination: subsId)
Run Code Online (Sandbox Code Playgroud)


Rob*_*ton 5

此 StompClientLib 库中的订阅功能不返回服务器将生成并返回给客户端的订阅 ID。你可以在这里看到代码:

https://github.com/WrathChaos/StompClientLib/blob/master/StompClientLib/Classes/StompClientLib.swift#L356

因此,您必须使用库中的其他 func 指定订阅 ID,并提供包含您选择的订阅 ID 的 stomp 标头:

public func subscribeWithHeader(destination: String, withHeader header: [String: String])
Run Code Online (Sandbox Code Playgroud)

例如:

var destination = "mytopic"
var ack = StompCommands.ackAuto
var subsId = "12345"
let header = [StompCommands.commandHeaderDestination: destination, StompCommands.commandHeaderAck: ack, StompCommands.commandHeaderDestinationId: subsId]
socketClient?.subscribeWithHeader(destination, header)
...
socketClient?.unsubscribe(subsId)
Run Code Online (Sandbox Code Playgroud)