FPGA FPGA - 1 year ago 147
Java Question

Embedded tomcat with java ee 7 websocket

i have this simple websocket

encoders = {WsCommandEncoder.class},
decoders = {WsCommandDecoder.class}
public class DataWebSocket {

and i initialize my embedded tomcat in the main function

public static void main(String[] args) throws Exception{

Tomcat tomcat = new Tomcat();

//how do i add the websocket to my tomcat instance?

String webDir = "/web";
tomcat.addWebapp("/", new File(webDir).getAbsolutePath());
System.out.println("configuring app with basedir: " + new File("./" + webDir).getAbsolutePath());


i have never used embedded tomcat before, usually there was the web.xml but i dont want to use webxml and i want to add the end point directly in code, so how do i add the websocket to my tomcat instance?

i am using Tomcat 8.0.9 and java ee 7

Answer Source

Take a look at the source code for the Tomcat unit tests - particularly those for WebSocket. There are lots of examples of adding endpoints via code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download