connect method

void connect()

Connects the socket.

Implementation

void connect() {
  if (conn != null) return;

  try {
    connState = SocketStates.connecting;
    final conn = transport(endPointURL(), headers);
    this.conn = conn;

    connState = SocketStates.open;
    _onConnOpen();
    conn.stream.timeout(Duration(milliseconds: longpollerTimeout));
    conn.stream.listen(
      (message) {
        // handling of the incoming messages
        onConnMessage(message as String);
      },
      onError: (error) {
        // error handling
        _onConnError(error);
      },
      onDone: () {
        // communication has been closed
        if (connState != SocketStates.disconnected) {
          connState = SocketStates.closed;
        }
        _onConnClose('');
      },
    );
  } catch (e) {
    /// General error handling
    _onConnError(e);
  }
}