新的 WebSocket APIs 例如:[code]public static void main(String[] args) {
SimpleWebSocketServer server = $.createWebSocketServer();
server.webSocket("/helloWebSocket")
.onConnect(conn -> conn.sendText("OK."))
.onText((text, conn) -> System.out.println("The server received: " + text))
.listen("localhost", 8080);
SimpleWebSocketClient client = $.createWebSocketClient();
client.webSocket("ws://localhost:8080/helloWebSocket")
.onText((text, conn) -> System.out.println("The client received: " + text))
.connect()
.thenAccept(conn -> conn.sendText("Hello server."));
}[/code]也增加了 Kotlin DSL 版 APIs,例如:[code]fun main(args: Array
val scheduler = Schedulers.createScheduler()
val p = Paths.get(HttpServer::class.java.getResource("/").toURI())
HttpServer {
router {
httpMethod = HttpMethod.GET
paths = listOf("/favicon.ico", "/static/*")
handler(StaticFileHandler(p.toAbsolutePath().toString()))
}
router {
httpMethod = HttpMethod.GET
path = "/"
asyncHandler { renderTemplate("template/websocket/index.mustache") }
}
webSocket("/helloWebSocket") {
onConnect {
val future = scheduler.scheduleAtFixedRate(
{ it.sendText("Current time: " + Date()) },
0, 1, TimeUnit.SECONDS)
it.onClose { future.cancel() }
}
onText { text, _ ->
println("Server received: " + text)
}
}
}.listen("localhost", 8080)
}[/code]这个例子中 WebSocket 服务和 HTTP 服务公用一个端口。
更新日志:
增加新的 WebSocket DSL APIs。
优化 HTTP 2 编解码性能。
修复了 HTTP1 Upgrade 机制时效的问题。
修复了 TCP Session 在并发调用 close 和 write 方法的情况下有可能无法正确关闭的问题。
软件详情:http://www.fireflysource.com/docs/websocket-server-and-client.html
下载地址:http://gitee.com/mirrors/Firefly-Framework
来自:开源中国社区

