用Websocket的Spring boot服务器和Python客户端

我正在使用一个由2个应用程序组成的堆栈,其中一个有后台,它要求有一个数据库,所以一个,并托管一个websocket服务器(Java / Spring Boot + React)。

第二个应用程序是一个Python应用程序,需要在后台的行动进行时得到通知。

因此,我这边的最佳解决方案是websocket,但当我试图使用Python连接到websocket服务器时,我面临一个问题。

代码详情

SpringServer configuration

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

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

Sring handler

@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 client

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 OK,也许我的服务器端配置有错误。我试着用withSockJS(),没有这个参数,也没有任何变化)。

有谁能在这个问题上帮助我?

非常感谢!


StackOverflow:Spring boot server and python client using Websocket - Stack Overflow