···4040 conn: *ws.Conn,
41414242 // You must define a public init function which takes
4343- pub fn init(h: ws.Handshake, conn: *ws.Conn, app: *App) !Handler {
4343+ pub fn init(h: *ws.Handshake, conn: *ws.Conn, app: *App) !Handler {
4444 // `h` contains the initial websocket "handshake" request
4545 // It can be used to apply application-specific logic to verify / allow
4646 // the connection (e.g. valid url, query string parameters, or headers)
···7171When you create a `websocket.Server(Handler)`, the specified `Handler` is your structure which will receive messages. It must have a public `init` function and `clientMessage` method. Other methods, such as `close` can optionally be defined.
72727373### init
7474-The `init` method is called with a `websocket.Handshake`, a `*websocket.Conn` and whatever app-specific value was passed into `Server(H).init`.
7474+The `init` method is called with a `*websocket.Handshake`, a `*websocket.Conn` and whatever app-specific value was passed into `Server(H).init`.
75757676When `init` is called, the handshake response has not yet been sent to the client (this allows your `init` method to return an error which will cause websocket.zig to send an error response and close the connection). As such, you should not use/write to the `*websocket.Conn` at this point. Instead, use the `afterInit` method, described next.
77777878The websocket specification requires the initial "handshake" to contain certain headers and values. The library validates these headers. However applications may have additional requirements before allowing the connection to be "upgraded" to a websocket connection. For example, a one-time-use token could be required in the querystring. Applications should use the provided `websocket.Handshake` to apply any application-specific verification and optionally return an error to terminate the connection.
79798080-The `websocket.Handshake` exposes the following fields:
8080+The `*websocket.Handshake` exposes the following fields:
81818282* `url: []const u8` - URL of the request in its original casing
8383* `method: []const u8` - Method of the request in its original casing
···85858686If you set the `max_headers` configuration value to > 0, then you can use `req.headers.get("HEADER_NAME")` to extract a header value from the given name:
87878888+If you set the `max_res_headers` configuration value to > 0, then you can set headers to be sent in the handshake response:
8989+9090+```zig
9191+pub fn init(h: *ws.Handshake, conn: *ws.Conn, app: *App) !Handler {
9292+ h.res_headers.add("set-cookie", "delicious")
9393+ //...
9494+}
9595+```
9696+9797+Note that, currently, the total length of the headers added to `res_headers` should not exceed 1024 characters, else you will exeperience an out of bounds segfault.
9898+8899```zig
89100// the last parameter, an *App in this case, is an application-specific
90101// value that you passed into server.listen()
9191-pub fn init(h: websocket.Handshake, conn: websocket.Conn, app: *App) !Handler {
102102+pub fn init(h: *websocket.Handshake, conn: websocket.Conn, app: *App) !Handler {
92103 // get returns a ?[]const u8
93104 // the name is lowercase
94105 // the value is in its original case