SockJS Python客户端 [英] SockJS Python Client

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

问题描述

我有一个依赖Websockets的网站(Java + Spring)(

I have a website (Java + Spring) that relies on Websockets (Stomp over Websockets for Spring + RabbitMQ + SockJS) for some functionality.

我们正在创建一个基于Python的命令行界面,我们想添加一些使用websockets已经可以使用的功能.

We are creating a command line interface based in Python and we would like to add some of the functionality which is already available using websockets.

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

Does anyone knows how to use a python client so I can connect using the SockJS protocol ?

PS_我知道没有测试过的简单库它没有订阅主题的能力

PS_ I am aware of a simple library which I did not tested but it does not have the capability to subscribe to a topic

PS2_正如我可以直接从python从RabbitMQ连接到 STOMP 并订阅主题但是直接暴露RabbitMQ感觉不对.关于第二种选择是否有任何评论?

PS2_ As I can connect directly to a STOMP at RabbitMQ from python and subscribe to a topic but exposing RabbitMQ directly does not feel right. Any comments around for second option ?

推荐答案

我使用的解决方案是不使用SockJS协议,而是使用纯Web套接字",并在Python中使用了websockets包并通过它使用踩踏程序包. stomper程序包仅生成消息"字符串,您只需使用ws.send(message)

The solution I used was to not use the SockJS protocol and instead do "plain ol' web sockets" and used the websockets package in Python and sending Stomp messages over it using the stomper package. The stomper package just generates strings that are "messages" and you just send those messages over websockets using ws.send(message)

服务器上的Spring Websockets配置:

Spring Websockets config on the server:

@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客户端代码上:

And on the Python client side of code:

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是我写的用于分析它的快速而肮脏的课程.

Now d will be a Stomp formatted message, which has a pretty simple format. MSG is a quick and dirty class I wrote to parse it.

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是随机生成的,不会被记住".但是,踩踏程序库使您能够创建退订消息.

This isn't the most complete solution. There isn't an unsubscribe and the id for the Stomp subscription is randomly generated and not "remembered." But, the stomper library provides you the ability to create unsubscribe messages.

所有发送到/something-to-subscribe-to的服务器端内容都将被预订的所有Python客户端接收.

Anything on the server side that is sent to /something-to-subscribe-to will be received by all the Python clients subscribed to it.

@Controller
public class SomeController {

    @Autowired
    private SimpMessagingTemplate template;

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

}

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

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