嘿,我们想使用 Django 来执行 python 代码并使用通道来获取结果。实现了 websocket 无法正常工作的所有内容。如果我尝试从 Angular 前端发送一些东西到 Django,它工作得很好。否则,我们的 Spring Boot StompSession Websockets 可以与其他 Spring Boot 应用程序和 Angular 正常工作。
但 Django 3.1.3 和 Spring Boot 2.3.3 没有。
另外还使用了channels 3.0.2、channels-redis 3.2.0和redis 6的docker容器。
我可以从 Django 服务器日志中看到:
WebSocket HANDSHAKING /ws/card-detection/ [127.0.0.1:53209]
i'm in connect method
WebSocket CONNECT /ws/card-detection/ [127.0.0.1:53209]
i'm in receive method
text_data: CONNECT
一旦 websocket 请求到来,就会执行此操作。
django 消费者(没有上面记录的打印):
class MyConsumer(WebsocketConsumer):
groups = ["broadcast"]
def connect(self):
self.accept()
def receive(self, text_data=None, bytes_data=None):
self.send(text_data="Hello world!")
def disconnect(self, close_code):
pass
我们从 Spring Boot 发送一个 base64 编码的图像。但其他一切也不起作用。正如你所看到的text_data
只是CONNECT
.
春季启动:
StompSessionHandler sessionHandler =
new CardDetectionStompSessionHandler(request, user, webSocketMessagingService);
createStompClient().connect(djangoServiceWSPath, sessionHandler);
}
private WebSocketStompClient createStompClient() {
WebSocketClient client = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
return stompClient;
}
stomp 会话处理程序可让您了解它的外观。它应该进入 afterConnected() 方法,但永远不会进入以下任何方法:
public class CardDetectionStompSessionHandler extends StompSessionHandlerAdapter {
private final ImageClassificationRequest request;
private final String username;
private final String destination = "/card-detection/";
private final WebSocketMessagingService webSocketMessagingService;
private StompSession session;
public CardDetectionStompSessionHandler(
ImageClassificationRequest request,
String username,
WebSocketMessagingService webSocketMessagingService) {
this.request = request;
this.username = username;
this.webSocketMessagingService = webSocketMessagingService;
}
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
log.info("Client: New session established: {}", session.getSessionId());
this.session = session;
session.subscribe("/user/queue/response", this);
log.info("Client: Subscribed to \"/user/queue/response\"");
session.send("/app" + destination, request);
log.info("Client: Message sent to {}", session.getSessionId());
}
@Override
public Type getPayloadType(StompHeaders headers) {
return ImageClassificationResponse.class;
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
log.info("Client: Message received");
ImageClassificationResponse response = (ImageClassificationResponse) payload;
webSocketMessagingService.sendToUser(username, response);
if (response.isDone()) {
session.disconnect();
log.info("Client: Disconnected.");
}
}
@Override
public void handleTransportError(StompSession session, Throwable exception) {
log.info("Client: Django is not available");
WebSocketResponse response = new WebSocketResponse();
response.setDisconnect(true);
webSocketMessagingService.sendToUser(username, response);
}
}
我们尝试将其更改为 Django 中的异步 Websocket,但这并没有改变任何事情。这self.accept
在姜戈斯connect()
方法似乎没有发送响应或者 Spring Boot 需要继续执行响应。