使用websocket的spring引导服务器和python客户端

zysjyyx4  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(321)

我使用了一个由两个应用程序组成的堆栈,一个是请求数据库的后台应用程序,另一个是托管websocket服务器(java/spring boot+react)
第二个应用程序是python应用程序,当后台正在进行操作时,需要通知它。
所以我这边最好的解决方案是websocket,但是当我尝试使用python在websocket服务器上连接时遇到了一个问题。
这是我的密码
springserver配置

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
    stompEndpointRegistry.addEndpoint("/ws").setAllowedOrigins("*");
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic", "/queue");
    registry.setApplicationDestinationPrefixes("/app");
}

sring处理程序

@Scheduled(fixedDelayString = "1000")
public void blastToClientsHostReport() {
    log.debug("Sending something on the websocket");
    messagingTemplate.convertAndSend("/topic/greeting", "Hello World");
}

@MessageMapping("/greeting")
public String handle(String message) {
    log.debug("Received message: $message");
    messagingTemplate.convertAndSend("/topic/greeting", message);
    String tm = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
    return "[" + tm + ": " + message + "]";
}

python客户端

websocket.enableTrace(True)

# Connecting to websocket

ws = websocket.create_connection("ws://localhost:8080/ws")

# Subscribing to topic

client_id = str(random.randint(0, 1000))
sub = stomper.subscribe("/topic/greeting", client_id, ack='auto')
ws.send(sub)

# Sending some message

ws.send(stomper.send("/app/greeting", "Hello there"))

while True:
    print("Receiving data: ")
    d = ws.recv()
    print(d)

但我得到了一个错误握手状态200好,可能是有一个错误,在我的服务器端配置(我尝试使用withsockjs(),如果没有这个参数,它不会改变任何东西)
有人能在这个问题上帮我吗?
谢谢!

5fjcxozz

5fjcxozz1#

我仍在研究这个问题,但没有找到解决我问题的方法。
我找到了解决方案的一部分,这是我能做的最接近的用例:python客户机不接收来自springwebsocket服务器的消息(没有任何答案)
@你找到解决办法了吗?

相关问题