{"id":270940,"date":"2023-05-07T09:00:21","date_gmt":"2023-05-07T01:00:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=270940"},"modified":"2023-04-30T14:42:05","modified_gmt":"2023-04-30T06:42:05","slug":"ktor-web-kotlin","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ktor-web-kotlin.html","title":{"rendered":"\u5f02\u6b65\u6846\u67b6Ktor 2.3 \u53d1\u5e03"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nKtor \u662f\u4e00\u4e2a\u5f02\u6b65\u6846\u67b6\uff0c\u7528\u4e8e\u521b\u5efa\u5fae\u670d\u52a1\u3001Web \u5e94\u7528\u7b49\u3002\u4ece\u5934\u5230\u5c3e\u90fd\u662f\u7528 Kotlin \u7f16\u5199\u7684\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

Ktor 2.3.0 \u8fd1\u65e5\u6b63\u5f0f\u53d1\u5e03\uff0c\u8fd9\u6b21\u66f4\u65b0\u4e3a Ktor \u751f\u6001\u5e26\u6765\u4e86\u8bb8\u591a\u65b0\u529f\u80fd\u548c\u6539\u8fdb\u3002\u4ee5\u4e0b\u90e8\u5206\u7b80\u8981\u4ecb\u7ecd\u4e86\u8be5\u7248\u672c\u7684\u4e3b\u8981\u53d8\u5316\uff1a<\/p>\n

\u6838\u5fc3\u6539\u8fdb<\/strong><\/div>\n
WebSockets ContentNegotiation \u63d2\u4ef6\u7684\u663e\u8bd5\u7c7b\u578b<\/strong><\/span><\/div>\n

WebSocket ContentNegotiation \u63d2\u4ef6\u73b0\u5728\u652f\u6301\u6307\u5b9a\u663e\u5f0f\u7c7b\u578b\uff1a<\/p>\n

client.webSocket(\"ws:\/\/localhost:8080\/echo\") {\r\n    val originalData: Any = Data(\"hello\")\r\n    sendSerialized(originalData, typeInfo())\r\n    val received = receiveDeserialized<\/data>(typeInfo<\/data>())\r\n\r\n    assertEquals(originalData, received)\r\n}<\/data><\/pre>\n
Socket \u7684\u7ed3\u6784\u5316\u5e76\u53d1\u652f\u6301<\/strong><\/span><\/div>\n

\u6765\u81ea ktor-network \u7684 Socket \u5b9e\u73b0\u73b0\u5728\u662f\u4e00\u4e2a CoroutineScope\u3002\u8fd9\u610f\u5473\u7740\u5b83\u652f\u6301 cancel() \u64cd\u4f5c\u6765\u7acb\u5373\u7ec8\u6b62\u8fde\u63a5\u3002<\/p>\n

close() \u7ee7\u7eed\u4f5c\u4e3a\u6b63\u5e38\u5173\u95ed\u5de5\u4f5c\uff0c\u5e76\u5c06\u7b49\u5f85\u6240\u6709\u5f85\u5b9a\u64cd\u4f5c\u5b8c\u6210\u3002<\/p>\n

\u629b\u5f03 JS \u9057\u7559\u914d\u7f6e<\/strong><\/span><\/div>\n

\u5373\u5c06\u53d1\u5e03\u7684 Kotlin 1.9.0 \u7248\u672c\u5c06\u5220\u9664\u9057\u7559\u7684 JS \u7f16\u8bd1\u5668\u3002\u4ece Ktor 2.3.0 \u5f00\u59cb\uff0c\u4f20\u7edf\u7684\u7f16\u8bd1\u5668\u4e0d\u518d\u88ab\u652f\u6301\u3002\u8bf7\u8003\u8651\u8fc1\u79fb\u5230\u65b0\u7684 IR \u7f16\u8bd1\u5668\u3002<\/p>\n

\u670d\u52a1\u5668<\/strong><\/div>\n
\u8def\u7531\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301<\/strong><\/span><\/div>\n

Ktor \u73b0\u5728\u5141\u8bb8\u5728\u5b9a\u4e49\u8def\u7531\u65f6\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8def\u7531\uff0c\u4f60\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u590d\u6742\u7684\u8def\u7531\uff0c\u5e76\u8f7b\u677e\u5730\u6355\u83b7\u8def\u7531\u53c2\u6570\u3002<\/p>\n

\r\nrouting {\r\n    get(Regex(\"^(?.+)$\")) {\r\n        val name = call.parameters[\"name\"]\r\n        call.respondText(\"Hello, $name!\")\r\n    }\r\n}<\/name><\/pre>\n
Static Content API \u6e05\u7406<\/strong><\/span><\/div>\n

Static Content API \u5df2\u7ecf\u88ab\u5b8c\u5584\u548c\u7cbe\u7b80\uff0c\u4ee5\u83b7\u5f97\u66f4\u53cb\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002\u8fd9\u6b21\u6211\u4eec\u628a\u91cd\u70b9\u653e\u5728\u6700\u5e38\u89c1\u7684\u60c5\u51b5\u4e0a\uff0c\u5e76\u4e3a\u5b83\u4eec\u505a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 API\uff1a<\/p>\n

\r\nrouting {\r\n    staticFiles(\"\/static\", File(\"files\"))\r\n}<\/pre>\n

\u5c06\u4e3a \/static \u8def\u5f84\u4e0b\u7684 files \u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u63d0\u4f9b\u670d\u52a1\u3002\u65e7\u7684 API \u4ecd\u7136\u53ef\u7528\uff0c\u5e76\u5c06\u88ab\u652f\u6301\u3002<\/p>\n

\u800c\u4e14\u6211\u4eec\u4e5f\u53ef\u4ee5\u4ece\u8d44\u6e90\u4e2d\u63d0\u4f9b\u6587\u4ef6\uff1a<\/p>\n

\u652f\u6301 CIO \u7684 100 Continue<\/p>\n

CIO \u5f15\u64ce\u73b0\u5728\u652f\u6301 100 Continue \u72b6\u6001\u3002\u5b83\u5f00\u7bb1\u5373\u7528\uff0c\u4e0d\u9700\u8981\u989d\u5916\u914d\u7f6e\u3002<\/p>\n

\u652f\u6301\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6<\/strong><\/span><\/div>\n

\u901a\u8fc7\u5141\u8bb8\u4f7f\u7528\u591a\u4e2a\u6587\u4ef6\uff0c\u914d\u7f6e\u7ba1\u7406\u5f97\u5230\u4e86\u7b80\u5316\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 -config CLI \u9009\u9879\u4f20\u9012\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002\u914d\u7f6e\u6587\u4ef6\u5c06\u88ab\u4f9d\u6b21\u52a0\u8f7d\u548c\u5408\u5e76\u3002<\/p>\n

\u652f\u6301 Jetty 11 \u548c Tomcat 10<\/strong><\/span><\/div>\n

\u5bf9\u4e8e\u5df2\u7ecf\u5305\u542b Jetty \u6216 Tomcat \u4f9d\u8d56\u5173\u7cfb\u7684\u9879\u76ee\uff0cKtor 2.3.0 \u589e\u52a0\u4e86\u5bf9 Jetty 11 \u548c Tomcat 10 \u670d\u52a1\u5668\u5b9e\u73b0\u7684\u652f\u6301\u3002\u5b83\u4eec\u662f\u4f5c\u4e3a\u5355\u72ec\u7684\u4f9d\u8d56\u9879\u4ea4\u4ed8\u7684\uff1a<\/p>\n

\r\ndependencies {\r\n    implementation(\"io.ktor:ktor-server-jetty-jakarta:2.3.0\")\r\n    implementation(\"io.ktor:ktor-server-tomcat-jakarta:2.3.0\")\r\n    implementation(\"io.ktor:ktor-server-servlet-jakarta:2.3.0\")\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

Ktor 2.3.0 \u8fd1\u65e5\u6b63\u5f0f\u53d1\u5e03\uff0c\u8fd9\u6b21\u66f4\u65b0\u4e3a Ktor \u751f\u6001\u5e26\u6765\u4e86\u8bb8\u591a\u65b0\u529f\u80fd\u548c\u6539\u8fdb\u3002\u4ee5\u4e0b\u90e8\u5206\u7b80\u8981\u4ecb\u7ecd\u4e86\u8be5\u7248\u672c […]<\/p>\n","protected":false},"author":668,"featured_media":270951,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[21],"tags":[],"class_list":["post-270940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/270940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=270940"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/270940\/revisions"}],"predecessor-version":[{"id":271206,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/270940\/revisions\/271206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/270951"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=270940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=270940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=270940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}