带有 Spring Boot Websockets (StompSession) 的 Django 通道不起作用

Django channels with Spring Boot Websockets (StompSession) do not work

提问人:CptDayDreamer 提问时间:11/18/2020 最后编辑:CptDayDreamer 更新时间:12/8/2020 访问量:674

问:

嘿,我们想使用 Django 来执行 python 代码并使用通道来获取结果。实现了 websocket 无法正常工作的所有内容。如果我尝试将一些东西从我们的 Angular 前端发送到 Django,它可以正常工作。否则,我们的 Spring Boot StompSession Websockets 可以与其他 Spring Boot 应用程序和 Angular 配合使用。

但是 Django 3.1.3 和 Spring Boot 2.3.3 没有。 此外,还使用了通道 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_dataCONNECT

Spring Boot:

     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;
   }

踩踏会话处理程序,让您了解它的外观。它应该进入 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,但这并没有改变任何事情。in Djangos 方法似乎没有发送响应或 Spring Boot 需要继续的响应。self.acceptconnect()

django spring-boot websocket stomp django 通道

评论

0赞 CptDayDreamer 11/19/2020
没人有想法吗?
0赞 Antoine Pinsard 11/20/2020
你的版本是什么?django-channels
0赞 CptDayDreamer 11/20/2020
@AntoinePinsard 3.0.2 和 channels-redis 3.2.0,我正在使用 redis 6 的 docker 容器
0赞 auvipy 9/9/2023
从你的问题来看,不清楚你是否真的将 django 通道公开为 API 或类似的东西。你能分享更多吗?

答: 暂无答案