PhoenixSocket constructor

PhoenixSocket(
  1. String endpoint, {
  2. PhoenixSocketOptions? socketOptions,
  3. WebSocketChannel webSocketChannelFactory(
    1. Uri uri
    )?,
})

Creates an instance of PhoenixSocket

endpoint is the full url to which you wish to connect e.g. ws://localhost:4000/websocket/socket

Implementation

PhoenixSocket(
  /// The URL of the Phoenix server.
  String endpoint, {
  /// The options used when initiating and maintaining the
  /// websocket connection.
  PhoenixSocketOptions? socketOptions,

  /// The factory to use to create the WebSocketChannel.
  WebSocketChannel Function(Uri uri)? webSocketChannelFactory,
})  : _endpoint = endpoint,
      _socketState = SocketState.unknown,
      _webSocketChannelFactory = webSocketChannelFactory {
  _options = socketOptions ?? PhoenixSocketOptions();

  _reconnects = _options.reconnectDelays;

  _messageStream =
      _receiveStreamController.stream.map(_options.serializer.decode);

  _openStream =
      _stateStreamController.stream.whereType<PhoenixSocketOpenEvent>();

  _closeStream =
      _stateStreamController.stream.whereType<PhoenixSocketCloseEvent>();

  _errorStream =
      _stateStreamController.stream.whereType<PhoenixSocketErrorEvent>();

  _subscriptions = [
    _messageStream.listen(_onMessage),
    _openStream.listen((_) => _startHeartbeat()),
    _closeStream.listen((_) => _cancelHeartbeat())
  ];
}