Home  Contents

Event callbacks

Websocket Core4 Lua Event System

SYNOPSIS

  1. function websocket:eventMessage(opcode, message) ... end
  2. function websocket:eventTextMessage(message) ... end
  3. function websocket:eventBinaryMessage(message) ... end
  4. function websocket:eventPongMessage(message) ... end
  5. function websocket:eventConnectionClose(status, reason) ... end

DESCRIPTION

To process data messages from the peer, the application must provide callback functions.

The callbacks are typically defined in a custom class that is derived from the base websocket class, as shown in the following example:

MyWebSocket = websocket:new() function MyWebSocket:eventTextMessage(msg) print("MESSAGE", msg:quoted()) end

The most generic is websocket:eventMessage(). The opcode and the message contents are passed directly from the peer. If defined, this callback will always be called first before any of the more specific callbacks.

The functions websocket:eventTextMessage(), websocket:eventBinaryMessage() and websocket:eventPongMessage() are shortcuts of the generic function. Messages with the opcodes to 1, 2 and 9, respectively, are passed to these handlers as per RFC6455.

The callback function websocket:eventConnectionClose() is called when a close message is received from the peer. After a close callback, no other messages will be received on this socket anymore.

Should the TCP socket close unexpectedly or report any other error, the implementation automatically generates a gratuitous call to websocket:eventConnectionClose() with the status code 1006 (unexpected close).