带有JavaEE7WebSocket的嵌入式tomcat
我有一个简单的网袋带有JavaEE7WebSocket的嵌入式tomcat,java,tomcat,Java,Tomcat,我有一个简单的网袋 @ServerEndpoint( value="/data", encoders = {WsCommandEncoder.class}, decoders = {WsCommandDecoder.class} ) public class DataWebSocket { //...impl } 我在main函数中初始化了嵌入式tomcat public static void main(String[] args) th
@ServerEndpoint(
value="/data",
encoders = {WsCommandEncoder.class},
decoders = {WsCommandDecoder.class}
)
public class DataWebSocket {
//...impl
}
我在main函数中初始化了嵌入式tomcat
public static void main(String[] args) throws Exception{
Tomcat tomcat = new Tomcat();
tomcat.setPort(6000);
//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());
tomcat.start();
tomcat.getServer().await();
}
我以前从未使用过嵌入式tomcat,通常有web.xml,但我不想使用webxml,我想直接在代码中添加端点,那么如何将websocket添加到我的tomcat实例中呢
我正在使用Tomcat 8.0.9和java ee 7查看Tomcat单元测试的源代码,特别是WebSocket的源代码。有很多通过代码添加端点的示例。这里是一个完整的示例。一旦你得到了正确的依赖关系,就要直截了当
我看了看,他们都是关于添加Servlet的,没有websocket的例子,所以你看起来不太努力。