简体   繁体   English

带有Java EE 7 WebSocket的嵌入式tomcat

[英]Embedded tomcat with java ee 7 websocket

i have this simple websocket 我有这个简单的网络套接字

@ServerEndpoint(
        value="/data",
        encoders = {WsCommandEncoder.class},
        decoders = {WsCommandDecoder.class}
)
public class DataWebSocket {
   //...impl
}

and i initialize my embedded tomcat in the main function 我在主要功能中初始化我的嵌入式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();


}

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? 我以前从未使用过嵌入式tomcat,通常有web.xml,但是我不想使用webxml,我想直接在代码中添加端点,那么如何将websocket添加到我的tomcat实例?

i am using Tomcat 8.0.9 and java ee 7 我正在使用Tomcat 8.0.9和Java EE 7

Take a look at the source code for the Tomcat unit tests - particularly those for WebSocket. 查看Tomcat单元测试的源代码,尤其是WebSocket的源代码。 There are lots of examples of adding endpoints via code. 有很多通过代码添加端点的示例。

Here is a complete example. 这是一个完整的例子。 straight forward once you get the dependencies right. 一旦正确地建立了依赖关系就可以了。

https://github.com/robmayhew/embedded-tomcat-websocket-example https://github.com/robmayhew/embedded-tomcat-websocket-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM