add: 增加undertow对websocket的支持

This commit is contained in:
qinerg 2017-12-21 16:51:21 +08:00
parent f127a429fd
commit a26e6b2eb5
3 changed files with 42 additions and 2 deletions

View File

@ -78,5 +78,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-websockets-jsr</artifactId>
<version>${undertow.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -48,7 +48,6 @@ import io.undertow.servlet.util.ImmediateInstanceFactory;
* Undertow 启动器
* @author qinerg(qinerg@gmail.com)
*/
@IocBean
public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace, LifeCycle, AppContextAware {
@ -115,7 +114,6 @@ public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace,
}
public void init() throws Exception {
String contextPath = getContextPath();
deployment = Servlets.deployment().setDeploymentName("nb").setClassLoader(classLoader).setEagerFilterInit(true).setSecurityDisabled(true);
@ -129,6 +127,7 @@ public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace,
}
addNutzSupport();
addWebSocketSupport();
deployment.addWelcomePages("index.html", "index.htm", "index.do");
@ -172,6 +171,14 @@ public class UndertowStarter implements ClassLoaderAware, IocAware, ServerFace,
addFilter(webFilter);
}
}
private void addWebSocketSupport() {
try {
WebSocketSupport.addWebSocketSupport(deployment, appContext.getPackage());
} catch (Error e) {
log.info("Not find undertow-websockets-jsr, websocket disable.");
}
}
public void addServlet(WebServletFace webServlet) {
if (webServlet == null || webServlet.getServlet() == null) {

View File

@ -0,0 +1,28 @@
package org.nutz.boot.starter.undertow;
import javax.websocket.server.ServerEndpoint;
import org.nutz.resource.Scans;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
/**
* websocket支持.
* 如果项目中不需要使用websocket则可以在自己项目的pom.xml中排除掉undertow-websockets-jsr即可
*
* @author qinerg(qinerg@gmail.com)
*/
public class WebSocketSupport {
public static void addWebSocketSupport(DeploymentInfo deployment, String packageName) {
WebSocketDeploymentInfo wsInfo = new WebSocketDeploymentInfo();
for (Class<?> klass : Scans.me().scanPackage(packageName)) {
if (klass.getAnnotation(ServerEndpoint.class) != null) {
wsInfo.addEndpoint(klass);
}
}
deployment.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, wsInfo);
}
}