简体   繁体   English

Spring 引导服务器和 python 客户端使用 Websocket

[英]Spring boot server and python client using Websocket

I'm using a stack of 2 applications, one with back-office which ask a database and so one, and hosting a websocket server (Java / Spring Boot + React)我正在使用一组 2 个应用程序,一个带有后台询问数据库等一个,并托管 websocket 服务器(Java / Spring Boot + React)

The second application is a Python app which need to be notified when an action is in progress on back-office.第二个应用程序是 Python 应用程序,当后台执行操作时需要通知该应用程序。

So the best solution on my side is websocket, but I'm facing a problem when try to connect on the websocket server using Python.所以我这边最好的解决方案是 websocket,但是当我尝试使用 Python 在 websocket 服务器上连接时遇到问题。

Here is my code这是我的代码

SpringServer configuration 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 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 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)

But I got an error Handshake status 200 OK , maybe there is a mistake on my server side configuration.但是我收到错误Handshake status 200 OK ,可能是我的服务器端配置有误。 (I try using withSockJS() and without this parameter, it doesn't change anything) (我尝试使用 withSockJS() 并且没有这个参数,它不会改变任何东西)

Anyone can help me on this topic?任何人都可以在这个话题上帮助我吗?

Thanks a lot !非常感谢 !

I'm still working on this issue, but without can't find a solution to my issue.我仍在研究这个问题,但找不到解决我的问题的方法。

I find a part of the solution, which is closest use case that I can do: Python client doesn't receive messages from Spring websocket server (without any answer)我找到了解决方案的一部分,这是我可以做的最接近的用例: Python client doesn't receive messages from Spring websocket server (without any answer)

@sampie777 did you find a solution? @sampie777 你找到解决方案了吗?

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

Have you tried to add /websocket to the endpoint?您是否尝试将 /websocket 添加到端点? It fixed the same issue in my case.在我的情况下,它解决了同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM