I want to setup a WebSocket Connection to upload a lot of very small files (1kb-50mb) to a web-application.
My Web-App runs in Wildfly, and the rest/https based components work fine, however my Websocket connection returns 404 everytime or 200 when I try.
Here's the code for my Endpoint:
package com.my.package;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.logging.Level;
import java.util.logging.Logger;
@ServerEndpoint("/smallfiles/socket")
public class WebsocketApi {
private static final long serialVersionUID = 2356475533479775725L;
private static final Logger logger = Logger.getLogger(WebsocketApi.class.getName());
private static Set<Session> clients = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void onOpen(Session session) {
// Set max buffer size
session.setMaxBinaryMessageBufferSize(1024 * 512);
// Add session to the connected sessions set
clients.add(session);
logger.log(Level.INFO,"Client " + session.getId() + " connected.");
}
@OnClose
public void onClose(Session session) {
// Remove session from the connected sessions set
clients.remove(session);
logger.log(Level.INFO,"Client " + session.getId() + " disconnected.");
}
@OnError
public void onError(Throwable error) {
logger.log(Level.SEVERE,error.getMessage());
}
@OnMessage
public void processUpload(byte[] uploadData, Session session) {
try {
if (session.isOpen()) {
// Wrap a byte array into a buffer
ByteBuffer buf = ByteBuffer.wrap(uploadData);
logger.log(Level.INFO,"Receiving Data");
//session.getBasicRemote().sendBinary(buf);
}
} catch (Exception e) {
try {
session.close();
} catch (Exception e1) {
logger.log(Level.SEVERE,"Failed to close Session. ",e1);
}
}
}
}
I had the Idea that maybe this is cuased by Apache, since our Wildfly is running behind an apache which works as reverse-proxy, so I added the Websocket Endpoints in my https-gw.conf:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName repName
SSLProxyEngine On
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
ProxyPass /Application/ http://127.0.0.1:8080/Application/
ProxyPassReverse /Application/ http://127.0.0.1:8080/Application/
ProxyPass /smallfiles/socket/ ws://127.0.0.1:8080/websocket/
ProxyPassReverse /smallfiles/socket/ ws://127.0.0.1:8080/websocket/
ProxyPass /Application/smallfiles/socket/ ws://127.0.0.1:8080/Application/websocket/
ProxyPassReverse /Application/smallfiles/socket/ ws://127.0.0.1:8080/Application/websocket/
SetEnv proxy-sendcl 10000000
SetEnv proxy-sendchunked
# Server Certificate:
SSLCertificateFile /etc/apache2/ssl.crt/host.net.cer
# Server Private Key:
SSLCertificateKeyFile /etc/apache2/ssl.key/host.net.key
# Certificate Authority (CA):
SSLCACertificateFile /etc/apache2/ssl.crt/my_ca.crt
</VirtualHost>
</IfModule>
However whenever I try to access the Websocket through Javascript, I get the following Problems:
ws = new WebSocket("wss://my.server.net/")
WebSocket {url: "wss://my.server.net/", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, …}
WebSocket connection to 'wss://my.application.net/' failed: Error during WebSocket handshake: Unexpected response code: 200
or
ws = new WebSocket("wss://my.server.net/smallfiles/socket")
WebSocket {url: "wss://my.server.net/smallfiles/socket", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, …}
WebSocket connection to 'wss://my.server.net/smallfiles/socket' failed: Error during WebSocket handshake: Unexpected response code: 404
I also checked the Post from User @cayz, but sadly I don't really see, what the users solution meant.
Does anyone know where I am having my blackage?
EDIT: I also checked the wildfly logs, and wildfly registers the endpoint.
2020-02-04 19:33:44,647 INFO [io.undertow.websockets.jsr] (ServerService Thread Pool -- 113) UT026003: Adding annotated server endpoint class com.my.package.WebsocketApi for path /smallfiles/socket