Websocket 客户端未收到任何消息 [英] Websocket Client not receiving any messages

查看:159
本文介绍了Websocket 客户端未收到任何消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Python 客户端,它打开与服务器的 websocket 连接并使用 STOMP 协议订阅特定主题,订阅进行得很好,正如我在服务器上看到的那样一切都很好.但是,当服务器发布一些消息时,客户端不会收到任何消息.以下是使用的代码:

I have Python client which opens a websocket connection to a server and subscribes to particular topic using STOMP protocol, subscription goes just fine as i see on the server all is fine. However, When the server publishes a few messages the client does not receive any. Here are the codes used:

客户

# coding: utf-8
import websocket
import stomp
import stomper
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInByaW5jaXBhbF9uYW1lIjoiYWRtaW4iLCJpc3MiOiJBdGhlbmEiLCJ1c2VydHlwZSI6IkxPQ0FMIiwiYW9zX3ZlcnNpb24iOiJldXBocmF0ZXMtNS4xMS1zdGFibGUiLCJyZWdpb24iOiJlbi1VUyIsImV4cCI6MTczNDI4MDI3NywidXVpZCI6ImI4MzhjOGRkLWI4NmQtNGNkZS05ZTE4LTUxM2E1OTk4ODhhYyIsImlhdCI6MTU3NjYwMDI3NywiYXV0aG9yaXRpZXMiOiJST0xFX0NMVVNURVJfQURNSU4sUk9MRV9NVUxUSUNMVVNURVJfQURNSU4sUk9MRV9VU0VSX0FETUlOLFJPTEVfQ0xVU1RFUl9WSUVXRVIiLCJqdGkiOiI1NTU1ZjEwZC04NGQ5LTRkZGYtOThhNC1mZmI1OTM1ZTQwZWEifQ.LOMX6ppkcSBBS_UwW9Qo2ieWZAGrKqADQL6ZQuTi2oieYa_LzykNiGMWMYXY-uw40bixDcE-aVWyrIEZQbVsvA"
headers = {"Authorization": "Bearer " + token}
uri = "ws://127.0.0.1:8765/notifications/websocket"
def on_msg(ws, msg):
    print(msg)

def on_error(ws, err):
    print(err)

def on_closed(ws):
    print("#Closed#")

def on_open(ws):
    sub = stomper.subscribe("/user/queue/alert", "MyuniqueId", ack="auto")
    ws.send(sub)

headers = {"Authorization": "Bearer " + token}



websocket.enableTrace(True)
ws = websocket.WebSocketApp(uri, header=headers, on_message=on_msg, on_error=on_error, on_close=on_closed)
ws.on_open = on_open
ws.run_forever()

用于发布消息的代码服务器:

Code server uses to publish the message:

    for (WatchesSubscription s : subscriptions) {
            template.convertAndSendToUser(s.getSession().getUser(), destination, dto);
        }

当我检查上述变量的值时,我看到目的地是预期的队列/警报.我也有 Java 客户端来测试,它工作得很好.我什至通过订阅 /topic/alerts 并通过 template.convertAndSend(/topic/alerts) 发送给它来尝试这个,在这里我也没有收到任何东西.我对此完全空白,希望得到任何帮助!

When i checked out the value of the above variables i saw that destination was as expected queue/alerts. I have java client to test out as well and it works just fine. I have even tried this by subscribing to /topic/alerts and sending to it via template.convertAndSend(/topic/alerts), here too i received nothing. I am a drawing a complete blank on this and would appreciate any sort of help!

推荐答案

折腾了好几天终于找到原因并解决了!

After many days of hair pulling I finally figured out the reason and the fix!

  1. 我使用的 Java 客户端是WebSocketStompClient stompClient = new WebSocketStompClient(transport);.stompClient.connect(URL, webSocketHttpHeaders, sessionHandler); 方法隐式发送一个stomp CONNECT\n\n\x00\n
  2. 已为 STOMP 配置的 Springboot 服务器将此理解为连接请求,并以 CONNECT_ACK 进行响应.
  3. 当这个 ACK​​ 被发送时,它也会用新用户更新它的本地 UserRegistry.因此内部消息代理知道有用户订阅了某某主题.
  4. 在我的 Python 代码中,我只是打开了一个 Websocket 连接,然后直接发送了一个 SUBSCRIBE 消息.所以经纪人从来没有得到一个CONNECT,所以用户永远不会被存储!这导致稍后发布的消息仅被代理丢弃.
  5. 修复方法是在打开连接之后和订阅之前发送一个 CONNECT\n\n\x00\n.这是代码:
  1. The java client I used was WebSocketStompClient stompClient = new WebSocketStompClient(transport);.The stompClient.connect(URL, webSocketHttpHeaders, sessionHandler); method implicitly sends a stomp CONNECT\n\n\x00\n
  2. The Springboot server which has been configured for STOMP understands this as a connection request and responds with a CONNECT_ACK.
  3. When this ACK is sent it also updates it's local UserRegistry with the new user. So the internal message broker knows that there is a user who has subscribed to so-and-so topic.
  4. In my Python code, i had merely opened a Websocket connection and after that directly sent a SUBSCRIBE message. So the broker never got a CONNECT so the user was never stored! This resulted in the messages later on being published to be merely discarded by the broker.
  5. The fix was to send a CONNECT\n\n\x00\n after opening up the connection and before the subscription. Here is the code:

def on_open(ws):
    #The magic happens here!
    ws.send("CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n")
    sub = stomper.subscribe("/user/queue/alert", "MyuniqueId", ack="auto")
    ws.send(sub)

这篇关于Websocket 客户端未收到任何消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆