Websocket configuration:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/patrol").withSockJS();
}
}
Endpoint:
@MessageMapping("/detection/face/frontal")
@SendTo("/topic/detection/face/frontal")
public String frontalFaceDetection(String encodedImage) throws IOException {
System.out.println("frontalFaceDetection was called");
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(encodedImage);
return "TEST";
}
With this configuration when I call the endpoint sending an encoded (base64) image as String
the endpoint returns:
java.lang.Exception: java.net.SocketException: Broken pipe
I tried changing the size of the message for the ServletServerContainerFactoryBean
adding this code in the WebSocketConfig
class:
@Bean
public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(Integer.MAX_VALUE);
container.setMaxBinaryMessageBufferSize(Integer.MAX_VALUE);
return container;
}
But now the error is:
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
I encoded the image on a client Android like this:
private String drawableToBase64(int resId) {
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), resId);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] ba = bao.toByteArray();
return Base64.encodeToString(ba, Base64.DEFAULT);
}
Debugging the code the size of the message is: 2622244
so I tried changing the size of the messages (server side) like this:
@Bean
public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(2622244 + 1);
container.setMaxBinaryMessageBufferSize(2622244 + 1);
return container;
}
Now I get
o.s.w.s.m.StompSubProtocolHandler : Failed to parse TextMessage payload=[SEND desti..], byteCount=2622291, last=true] in session ccfa1dfa-7076-395d-9c26-e0a8d106ba81. Sending STOMP ERROR to client. org.springframework.messaging.simp.stomp.StompConversionException: The configured STOMP buffer size limit of 65536 bytes has been exceeded
I tried the solution proposed in this SO thread too, no more memory related errors but i get again:
java.lang.Exception: java.net.SocketException: Broken pipe
Anyone have experience with this issue? I'm trying different solutions but it doesn't work. Where I'm wrong? Any idea?
Thank you.