Built-in Functions
Structures
Built-in Variables and Constants
Conventions
Exchanges
Server.close
Stops accepting new connections; handlers already running finish normally (graceful shutdown).
close()Examples
Start an HTTP service on a random port, watch the pending requests, then shut it down gracefully.
javascript
function main() {
// Port 0 picks a free port; addr() returns the actual address
var server = threading.Serve("http://127.0.0.1:0", function (ctx) {
Sleep(1000)
ctx.write("done")
})
Log("listen on", server.addr())
// Output: listen on 127.0.0.1:<random port>
// Send a request from another thread while the main thread watches the server
threading.Thread(function (addr) {
Log("response:", HttpQuery("http://" + addr + "/"))
// Output: response: done (about 1 second later)
}, server.addr())
Sleep(200)
Log("pending:", server.pending())
// Output: pending: 1 (one request is being handled)
// Stop accepting connections; requests in flight run to completion
server.close()
Log("idle:", server.join(5000))
// Output: idle: true (the server is closed and every request has finished)
}See Also
Remarks
Calling it again has no effect. To wait for the running handlers, call join() afterwards.