Home Contents | # websocket:attachServer | Websocket | Core4 Lua Event System

## SYNOPSIS

1. ok = websocket:attachServer(app, socket, body)
2. ok = websocket:attachClient(app, socket, body)
3. websocket:detach()

## DESCRIPTION

The websocket implementation does not include the initial HTTP handshake. Once the application has completed the handshake, it must call one of the attach functions to pass the socket over to the websocket implementation.

The attachment functions cause a websocket object to take over the socket communication. After attachment, the application must no longer directly read or write from the socket. All further communication must be done via the websocket API.

The handler is attached to the event processor passed in app.

The argument socket must be an already connected TCP socket with the WebSockets Upgrade handshake already completed.

During handshake processing it is common that there has already some non-header data been buffered by the application: Everything that follows after the double newline received from the peer. This data is in fact belonging to the websocket stream. The caller must pass the leftover bytes in the body parameter. This parameter must be present, in order to avoid mistakes. In special cases where it is sure that there will never be initial body data, pass an empty string.

By calling websocket:detach(), the websocket code immediately releases control of the attached socket. No automatic close message is sent, this must be done by the application before, if required. It is safe to call websocket:detach() while no socket is attached.

## RETURN VALUE

On attach, returns boolean true/false to indicate success.

The detach function returns nothing.

## SEE ALSO

websocket:sendClose(), websocket:new(), websocket:close(), websocket:send()