问题
Hey there we want to use Django just to execute python code and use channels for the results. Implemented everything the websockets are not working as they should. If I try to send something from our Angular frontend to Django it works fine. And otherwise our Spring Boot StompSession Websockets work fine with other Spring Boot Applications and Angular.
But Django 3.1.3 and Spring Boot 2.3.3 do not. Additionally channels 3.0.2, channels-redis 3.2.0 and a docker container of redis 6 are used.
What I can see from the Django server logs:
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
This gets executed as soon as the websocket request comes in.
The django consumer (without the prints that are logged above):
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
From Spring Boot we send an base64 encoded image. But also everything else does not work. As you can see the text_data
is just CONNECT
.
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;
}
The stomp session handler to give you an idea how it looks. It should go in the afterConnected() method but it will never go in any of this methods:
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);
}
}
We tried to change it to an async websocket in Django but that didn't changed a thing. The self.accept
in Djangos connect()
method does not seem to send a response or the response Spring Boot needs to go on.
来源:https://stackoverflow.com/questions/64886685/django-channels-with-spring-boot-websockets-stompsession-do-not-work