Trying to set up undertwo to play with it a bit, i am trying to server a static html file index.html and add my annoted websocket ServerEndpoint pojo so i did the following
final Xnio xnio = Xnio.getInstance("nio", Undertow.class.getClassLoader());
final XnioWorker xnioWorker = xnio.createWorker(OptionMap.builder().getMap());
WebSocketDeploymentInfo webSockets = new WebSocketDeploymentInfo ()
.addEndpoint(Socket.class)
.setWorker(xnioWorker);
final DeploymentManager deployment = defaultContainer()
.addDeployment(deployment()
.setClassLoader(main.class.getClassLoader())
.setContextPath("/websockets")
.setDeploymentName("websockets")
.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, webSockets));
deployment.deploy();
deployment.start();
Undertow.builder()
.addHttpListener(8000,"localhost")
.setHandler(path().addPrefixPath("/", resource(new ClassPathResourceManager(main.class.getClassLoader(), main.class.getPackage())).addWelcomeFiles("WSGSS/index.html")))
.build()
.start();
the index.html gets served as expected but i cant seem to connect to the websocket, and i am sure i am doing something wrong with the way i deploy the websocket, if i do it as in the examples on github it works but then i cant find the right way to serve the static files
in javascript i get the exception
var ws = new WebSocket("ws://localhost:8000/websockets/socket")
WebSocket connection to 'ws://localhost:8000/websockets/socket' failed: Error during WebSocket handshake: Unexpected response code: 404
also here is my ServerEndpoint
@ServerEndpoint(value = "/socket",
encoders = CommandEncoder.class,
decoders = CommandDecoder.class)
public class Socket{
private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
private final Logger log = Logger.getLogger(getClass().getName());
@OnMessage
public void onMessage(Session session,Command command){
log.info("command of type "+ command.getAction() + " recieved");
command.Process();
try{
session.getBasicRemote().sendObject(command);
//Future<Void> future = session.getAsyncRemote().sendText(message);
} catch (IOException | EncodeException ex){
log.info(ex.getMessage() + ex.getCause());
}
}
@OnOpen
public void onOpen(Session session){
sessions.add(session);
log.info("new session created "+ session.getId());
}
@OnClose
public void onClose(Session session){
sessions.remove(session);
}
@OnError void onError(Session session,Throwable thr){
log.info(session.getId() + " error " + thr.getMessage() + thr.getCause());
}
}