【问题标题】:SockJS Python ClientSockJS Python 客户端
【发布时间】:2016-04-07 01:46:28
【问题描述】:

我有一个网站 (Java + Spring),它依赖于 Websockets (Stomp over Websockets for Spring + RabbitMQ + SockJS) 来实现某些功能。

我们正在创建一个基于 Python 的命令行界面,并且我们希望添加一些使用 websockets 已经可用的功能。

有谁知道如何使用 python 客户端以便我可以使用 SockJS 协议进行连接?

PS_ 我知道simple library 我没有测试过,但它没有订阅主题的能力

PS2_ 因为我可以直接连接到 STOMP at RabbitMQ from python 并订阅主题,但直接公开 RabbitMQ 感觉不对。有没有第二个选项的cmets?

【问题讨论】:

  • 你最终为此做了什么?
  • @Jeef 我们找不到好的解决方案,所以我们不得不通过额外的 API 模拟该功能。
  • @Tk421 我们遇到了将 python 客户端连接到 SockJS + Spring 的相同问题。我们试图在 python 中使用 websocket 库。例如 ws = websocket.WebSocketApp("ws://localhost:8080/socket_name/topic_name/1/websocket", 。我们能够连接到 websocket 但没有收到发送到主题的消息。我们需要添加春季有任何自定义的握手处理程序来实现这一点吗?
  • @RajaVikram 我发布了一个工作示例,作为我使用 websockets 和 Stomp 与 Python 客户端与 Spring websockets 服务器对话的答案。希望对您有所帮助。

标签: python spring rabbitmq stomp spring-websocket


【解决方案1】:

我已经回答了从 Springboot 服务器发送一个带有 sockJs 回退到 Python 客户端通过 websockets 发送 STOMP 消息的特定问题:Websocket Client not receiving any messages。它还解决了上述的 cmets

  1. 发送给特定用户。
  2. 为什么客户端没有收到任何消息。

【讨论】:

    【解决方案2】:

    我使用的解决方案是不使用 SockJS 协议,而是使用“plain ol' web sockets”并在 Python 中使用 websockets 包并使用 stomper 包通过它发送 Stomp 消息。 stomper 包只生成“消息”字符串,您只需使用ws.send(message)通过websockets 发送这些消息

    服务器上的 Spring Websockets 配置:

    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    
        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            registry.addEndpoint("/my-ws-app"); // Note we aren't doing .withSockJS() here
        }
    
    }
    

    在代码的 Python 客户端:

    import stomper
    from websocket import create_connection
    ws = create_connection("ws://theservername/my-ws-app")
    v = str(random.randint(0, 1000))
    sub = stomper.subscribe("/something-to-subscribe-to", v, ack='auto')
    ws.send(sub)
    while not True:
        d = ws.recv()
        m = MSG(d)
    

    现在d 将是一个 Stomp 格式的消息,它的格式非常简单。 MSG 是我为解析它而编写的一个快速而肮脏的类。

    class MSG(object):
        def __init__(self, msg):
            self.msg = msg
            sp = self.msg.split("\n")
            self.destination = sp[1].split(":")[1]
            self.content = sp[2].split(":")[1]
            self.subs = sp[3].split(":")[1]
            self.id = sp[4].split(":")[1]
            self.len = sp[5].split(":")[1]
            # sp[6] is just a \n
            self.message = ''.join(sp[7:])[0:-1]  # take the last part of the message minus the last character which is \00
    

    这不是最完整的解决方案。没有取消订阅,Stomp 订阅的 id 是随机生成的,而不是“记住的”。但是,stomper 库为您提供了创建退订消息的能力。

    服务器端发送到/something-to-subscribe-to 的任何内容都将被所有订阅它的 Python 客户端接收。

    @Controller
    public class SomeController {
    
        @Autowired
        private SimpMessagingTemplate template;
    
        @Scheduled(fixedDelayString = "1000")
        public void blastToClientsHostReport(){
                template.convertAndSend("/something-to-subscribe-to", "hello world");
            }
        }
    
    }
    

    【讨论】:

    • 如何向特定端点发送消息?我试过这个: ws = create_connection("ws://host:port/prefix", header=["Authorization:Token"]) pub = stomper.send('/app/connected', '嘿服务器,我是client') ws.send(pub) 但是,我的服务器收到错误“消息中没有用户标头”
    猜你喜欢
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2017-05-19
    • 2016-03-21
    相关资源
    最近更新 更多