首页 文章

使用STOMP订阅主题Spring WebSocket时未创建动态队列?

提问于
浏览
6

我正在为订阅特定事件的所有用户子集开发推送通知 . 用户使用以下格式订阅RabbitMQ中的主题:user-id.event-type.id . 我使用Spring Websocket,Stomp,RabbitMQ和前端SockJS和Angular JS . 应通知用户有关事件的所有操作(注释等,日期更改) .

到目前为止我们有什么:

首先,我通过REST webservice endpoints 进行身份验证,并将我的令牌放到Cookie中 . 然后我们连接到websocket . 用户订阅主题(/topic/user-45.meeting.1235)并获得通知 . 但我的问题是有些用户没有收到通知 . 对于第二个用户,由于某种原因,未在RabbitMQ中创建队列 . 谁知道为什么?

这是我在Spring applicationContext.xml中的代理设置:

<websocket:message-broker application-destination-prefix="/app">
        <websocket:stomp-endpoint path="/stomp">
            <websocket:sockjs/>
        </websocket:stomp-endpoint>
        <websocket:stomp-broker-relay relay-host="localhost" relay-port="61613" system-login="guest" system-passcode="guest" prefix="/queue, /topic"/>
    </websocket:message-broker>

这是通过Sockjs订阅的方式:

var ws = new SockJS('http://' + location.host + path);
var stompClient = Stomp.over(ws);
stompClient.connect({
    username: '',
    password: '',
    host: '/'
}, function () {
    stompClient.subscribe('/topic/user-45.meeting.' + obj.id,
        function (message) {
            console.log(message);
        }, {
            persistent: true
        });
});

更新

如果我们在SUBSCRIBE帧中指定唯一的Id字段,它会为每个用户创建唯一的队列 . 是这样的吗?

1 回答

  • 1

    据我所知,您需要订阅 \queue 而不是 \topic . 通过这样做,您不需要为不同的用户自定义主题名称,这将根据已登录的用户由sockjs处理 . 在服务器端,您还可以发送消息给特定用户使用 \queue\user\{username}\{name of queue}

相关问题