Types
Message = object kind*: MessageKind data*: string
- Source Edit
MessageKind = enum TextMessage, BinaryMessage, Ping, Pong
- Source Edit
Request = ptr RequestObj
- Source Edit
RequestHandler = proc (request: Request) {....gcsafe.}
- Source Edit
RequestObj = object httpVersion*: HttpVersion ## HTTP version from the request line. httpMethod*: string ## HTTP method from the request line. uri*: string ## Raw URI from the HTTP request line. path*: string ## Decoded request URI path. queryParams*: QueryParams ## Decoded request query parameter key-value pairs. pathParams*: PathParams ## Router named path parameter key-value pairs. headers*: HttpHeaders ## HTTP headers key-value pairs. body*: string ## Request body. remoteAddress*: string ## Network address of the request sender.
- Source Edit
WebSocketEvent = enum OpenEvent, MessageEvent, ErrorEvent, CloseEvent
- Source Edit
WebSocketHandler = proc (websocket: WebSocket; event: WebSocketEvent; message: Message) {....gcsafe.}
- Source Edit
Procs
proc newServer(handler: RequestHandler; websocketHandler: WebSocketHandler = nil; logHandler: LogHandler = nil; workerThreads = max(countProcessors() * 10, 1); maxHeadersLen = 8 * 1024; maxBodyLen = 1024 * 1024; maxMessageLen = 64 * 1024): Server {....raises: [MummyError], tags: [], forbids: [].}
- Creates a new HTTP server. The request handler will be called for incoming HTTP requests. The WebSocket handler will be called for WebSocket events. Calls to the HTTP, WebSocket and log handlers are made from worker threads. WebSocket events are dispatched serially per connection. This means your WebSocket handler must return from a call before the next call will be dispatched for the same connection. Source Edit
proc serve(server: Server; port: Port; address = "localhost") {. ...raises: [MummyError], tags: [WriteIOEffect, ReadIOEffect, RootEffect, TimeEffect], forbids: [].}
- The server will serve on the address and port. The default address is localhost. Use "0.0.0.0" to make the server externally accessible (with caution). This call does not return unless server.close() is called from another thread. Source Edit
proc upgradeToWebSocket(request: Request): WebSocket {....raises: [MummyError], gcsafe, tags: [RootEffect], forbids: [].}
- Upgrades the request to a WebSocket connection. You can immediately start calling send(). Future updates for this WebSocket will be calls to the websocketHandler provided to newServer. The first event will be onOpen. Note: if the client disconnects before receiving this upgrade response, no onOpen event will be received. Source Edit
proc waitUntilReady(server: Server; timeout: float = 10.0) {. ...raises: [MummyError], tags: [TimeEffect], forbids: [].}
- This proc blocks until the server is ready to receive requests or the timeout has passed. The timeout is in floating point seconds. This is useful when writing tests, where you need to know the server is ready before you begin sending requests. If the server is already ready this returns immediately. Source Edit