Hi all, I’m trying make Websockets work with embedded Tomcat (8.0.28) started by Main class (shown below). When I try to open websocket connection from browser using the URLs below, I always get “Error during WebSocket handshake: Unexpected response code: 404”. Is there anything wrong with the way Tomcat is started/setup or classpath used in running the application?
Any help is greatly appreciated. -Madhav WS URLs getting the error: http://localhost:8080/entities http://localhost:8080/ws2/entities Http URLs that show html page (but are not successful in connecting to websocket): http://localhost:8080/ws2/hello Main class that starts Tomcat: package com.mypackage; public class TomcatWebServer { public static void main(String[] args) { Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); Context ctx = tomcat.addContext("/ws2", new File(".").getAbsolutePath()); Set<Class<?>> classes = new HashSet<>(); classes.add(MyWebSocketEndpoint.class); ctx.addServletContainerInitializer(new WsSci(), classes); Tomcat.addServlet(ctx, "hello", new HttpServlet() { protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Writer w = resp.getWriter(); w.write("<html><head><script>" + "var wsUri=\"ws://localhost:8080/ws2/entities\";" + "var ws=new WebSocket(wsUri);" + "ws.onopen=function(evt){console.log(\"opened: \"+JSON.stringify(evt));};" + "ws.onclose=function(evt){console.log(\"closed: \"+JSON.stringify(evt));};" + "</script></head><body><h1>Hello, World!</h1></body></html>"); w.flush(); } }); ctx.addServletMapping("/hello", "hello"); try { tomcat.start(); tomcat.getServer().await(); } catch (LifecycleException e) { e.printStackTrace(); } } } WebSocket implementing Class: @ServerEndpoint("/entities") public class MyWebSocketEndpoint { private Map<String, Session> clientSessions = new HashMap<>(); @OnOpen public void onOpen(Session session) { System.out.println("session opened = " + session.getId()); clientSessions.put(session.getId(), session); } @OnMessage public void onMessage(String message) { System.out.println("message = " + message); } @OnClose public void onClose(Session session) { System.out.println("session close = " + session.getId()); clientSessions.remove(session.getId()); } void broadcast(String message) { for (Session session : clientSessions.values()) { session.getAsyncRemote().sendText(message); } } } Gradle build file: apply plugin: 'java' apply plugin: 'idea' repositories { mavenCentral() } configurations { provided } sourceSets { main { compileClasspath += configurations.provided } } idea { module { scopes.PROVIDED.plus += [configurations.provided] } } task run() { dependsOn build doLast { javaexec { main = 'com.mypackage.TomcatWebServer' classpath = sourceSets.main.output + sourceSets.main.runtimeClasspath println 'classpath for running ' + main + ' is ' + classpath.getAsPath() } } } dependencies { compile 'log4j:log4j:1.2.17' compile 'org.apache.tomcat.embed:tomcat-embed-core:8.0.28' compile 'org.apache.tomcat.embed:tomcat-embed-logging-log4j:8.0.28' compile 'org.apache.tomcat.embed:tomcat-embed-websocket:8.0.28' compile 'org.apache.tomcat:tomcat-websocket-api:8.0.28' // provided 'javax.websocket:javax.websocket-api:1.1' } © Copyright 2015 REDI Global Technologies LLC (“REDI”), member FINRA, SIPC. All rights reserved. The information contained in and accompanying this communication may be confidential, subject to legal privilege, or otherwise protected from disclosure, and is intended solely for the use of the intended recipient(s). If you are not the intended recipient of this communication, please delete and destroy all copies in your possession, notify the sender that you have received this communication in error, and note that any review or dissemination of, or the taking of any action in reliance on, this communication is expressly prohibited. E-mail messages may contain computer viruses or other defects, may not be accurately replicated on other systems, or may be intercepted, deleted or interfered with without the knowledge of the sender or the intended recipient. REDI makes no warranties in relation to these matters. Please note that REDI reserves the right to intercept, monitor, and retain e-mail messages to and from its systems as permitted by applicable law. If you are not comfortable with the risks associated with e-mail messages, you may decide not to use e-mail to communicate with REDI.