本人特别喜欢在折腾这件事上浪费生命!
手上有一个基于Maven的Web项目,Web框架用的Nutz,最近不是Nutzmore出了新版,我就看了一下,发现这个Undertow很厉害的样子,就打算试一试。
那么淌坑的经历就开始了。
首先,我用了WebSocket,插件(Nutz-plugins-undertow)并没有特别说明用法,看了一下源码,最后在WebLancher的start方法里,自己添加了一个WebSocket的HttpHandler。
-----------小分分--------------
//添加WebSocket的Handler
PathHandler pathHandler = Handlers.path().addPrefixPath("/myWebsocket", websocket(new WebSocketConnectionCallback() {
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
//这就是服务端向客户端发送消息的方法
WebSockets.sendText(message.getData(), channel, null);
}
});
channel.resumeReceives();
}
})).addPrefixPath(contextPath, servletHandler);
------------小分分----------------
然后还算顺利,到了打包的时候了,作者提供了一个相关的Pom文件例子和命令,不过因为我项目里还夹杂了一些Kotlin代码(好奇心忒重),我添加了Kotlin的编译相关的配置,不过一直没办法把页面及其相关的东西放进去,尝试过多次,最后只有自己配置一个webRoot地址了,也可以写死,随你便。 具体的pom文件如下:
<build>
<sourceDirectory>src/main/org</sourceDirectory>
<finalName>ROOT</finalName>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${org.jetbrains.kotlin.kotlin-stdlib-jre8.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your main class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
最后执行
mvn clean package -U
就可以了 然后
java -jar xx.jar "你的web目录"
最后说明一下,这个“你的web目录” 是传递给WebConfig里的setRoot方法的,你可以用你喜欢的方式来插入!
还有其他坑,遇到再补充了。