在运行时映射websockets端点(Mapping websockets endpoints at runtime)

编程入门 行业动态 更新时间:2024-10-28 06:30:59
在运行时映射websockets端点(Mapping websockets endpoints at runtime)

我知道我们可以在servlet api 3.0之后的运行时映射一个servlet,可以实现如下:

@Override public void contextInitialized( ServletContextEvent sce ) { ServletContext sc = sce.getServletContext(); String servletMapping = "/yourURL"; ServletRegistration sr = sc.addServlet( servletMapping, "org.yourdomain.yourclass" ); sr.setInitParameter( "key", "value" ); sr.addMapping( servletMapping ); }

有没有类似的方式来做到这一点与websockets(使用javax.websockets.api)?

I know we can map a servlet at runtime since servlet api 3.0, that can be achieved as below:

@Override public void contextInitialized( ServletContextEvent sce ) { ServletContext sc = sce.getServletContext(); String servletMapping = "/yourURL"; ServletRegistration sr = sc.addServlet( servletMapping, "org.yourdomain.yourclass" ); sr.setInitParameter( "key", "value" ); sr.addMapping( servletMapping ); }

Is there a similar way of doing this with websockets (using javax.websockets.api)?

最满意答案

ServletContainerInitializer的Websocket相当于ServerApplicationConfig 。 它只需要一个服务文件,Websocket API将主动扫描WAR中的WAR和JAR,以实现ServerApplicationConfig接口的任何类并使用它们。 您可以在getEndpointConfigs()使用ServerEndpointConfig.Builder以编程方式构建Websocket端点并将其返回。

以下是一个YourEndpoint.class例子,假设YourEndpoint.class表示您希望以编程方式添加的Websocket端点,并且您希望忽略任何扫描的类。

public class YourServerApplicationConfig implements ServerApplicationConfig { @Override public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scannedClasses) { Set<ServerEndpointConfig> configs = new HashSet<>(); configs.add(ServerEndpointConfig.Builder.create(YourEndpoint.class, "/yourPath").build()); return configs; } @Override public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scannedClasses) { return Collections.emptySet(); } }

The Websocket equivalent of ServletContainerInitializer is the ServerApplicationConfig. It only doesn't require a services file, the Websocket API will proactively scan the WAR and JARs in WAR for any classes implementing ServerApplicationConfig interface and use them. You can in getEndpointConfigs() use ServerEndpointConfig.Builder to programmatically build a Websocket endpoint and return it.

Here's a kickoff example, given that YourEndpoint.class represents the Websocket endpoint you'd like to programmatically add, and you'd like to ignore any scanned classes.

public class YourServerApplicationConfig implements ServerApplicationConfig { @Override public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scannedClasses) { Set<ServerEndpointConfig> configs = new HashSet<>(); configs.add(ServerEndpointConfig.Builder.create(YourEndpoint.class, "/yourPath").build()); return configs; } @Override public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scannedClasses) { return Collections.emptySet(); } }

更多推荐

本文发布于:2023-08-07 18:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mapping   websockets   runtime   endpoints

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!