# Class RAISocket

- Defined in: rai.js

Class Summary
Constructor Attributes | Constructor Name and Description
--- | ---
| RAISocket(socket, options) Creates a instance for interacting with a client (socket) Optional options object is the same that is passed to the parent RAIServer object Events 'command' - emitted if a client sends a command.

## Method Summary

## Class Detail

RAISocket(socket, options)

Creates a instance for interacting with a client (socket)

Optional options object is the same that is passed to the parent RAIServer object

Events

- 'command' - emitted if a client sends a command. Gets two params - command (String) and payload (Buffer)
- 'data' - emitted when a chunk is received in data mode, the param being the payload (Buffer)
- 'ready' - emitted when data stream ends and normal command flow is recovered
- 'tls' - emitted when the connection is secured by TLS
- 'error' - emitted when an error occurs. Connection to the client is disconnected automatically. Param is an error object.
- 'timeout' - emitted when a timeout occurs. Connection to the client is disconnected automatically if disconnectOnTimeout option is set to true.
- 'end' - emitted when the client disconnects

Parameters:
{Object} socket
  Socket for the client
{Object} options Optional
  Optional options object

## Method Detail

- <private> _destroy()

  Called when the connection is or is going to be ended

- <private> _processData(str)

  Processed incoming command lines and emits found data as `'command'` with the command name as the first param and the rest of the data as second (Buffer)

  Parameters:
  {String} str
    Binary string to be processed
- end()

  Closes the connection to the client

- send(data)

  Sends some data to the client. `<CR><LF>` is automatically appended to the data

  Parameters:
  {String|Buffer} data
    Data to be sent to the client
- startDataMode(sequence)

  Instructs the server to be listening for mixed data instead of line based commands

  Parameters:
  {String} sequence Optional, Default: "."
    - optional sequence on separate line for matching the data end
- startTLS(credentials, callback)

  Instructs the server to upgrade the connection to secure TLS connection

  Fires `callback` on successful connection upgrade if set, otherwise emits `'tls'`

  Parameters:
  {Object} credentials Optional
    An object with PEM encoded key and certificate `{key:"---BEGIN...", cert:"---BEGIN..."}`, if not set autogenerated values will be used.
  {Function} callback Optional
    If calback is set fire it after successful connection upgrade, otherwise `'tls'` is emitted

## Event Detail

<private> _onClose(hadError)

Called when the connection is closed

Parameters:
{Boolean} hadError
  did the connection end because of an error?

<private> _onEnd()

Called when the connection is ended. Emits `'end'`

<private> _onError(err)

Called when an error has appeared. Emits `'error'` with the error object as a parameter.

Parameters:
{Object} err
  Error object

<private> _onReceiveData(chunk)

Called when a chunk of data arrives from the client. If currently in data mode, transmit the data otherwise send it to `_processData`

Parameters:
{Buffer|String} chunk
  Data sent by the client

<private> _onTimeout()

Called when a timeout has occured. Connection will be closed and `'timeout'` is emitted.