我有这个简单的 websocket
@ServerEndpoint(
value="/data",
encoders = {WsCommandEncoder.class},
decoders = {WsCommandDecoder.class}
)
public class DataWebSocket {
//...impl
}
然后我在主函数中初始化我的嵌入式 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 的源代码。有很多通过代码添加端点的示例。
关于java - 带有 java ee 7 websocket 的嵌入式 tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24560917/