1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import net.http.* import std.time.* import std.sync.*
func startServer(): Unit { // 1. 构建 Server 实例 let server = ServerBuilder() .addr("127.0.0.1") .port(8080) .build() // 2. 注册请求处理逻辑 server.distributor.register("/hello", {httpContext => httpContext.responseBuilder.body("Hello Cangjie!") }) // 3. 启动服务 server.serve() }
func startClient(): Unit { // 1. 构建 client 实例 let client = ClientBuilder().build() // 2. 发送 request let resp = client.get("http://127.0.0.1:8080/hello") // 3. 读取response println(resp) // 4. 关闭连接 client.close() }
main () { spawn { startServer() } sleep(Duration.second) startClient() }
|