SockJSClient constructor
SockJSClient(
- Uri uri, {
- SockJSOptions? options,
Constructs a new SockJSClient that will attempt to connect to a SockJS
server at the given uri
.
Additional configuration can be provided via options
:
SockJSOptions.server
- string to append to url for actual data connection. Defaults to a random 4 digit number.SockJSOptions.transports
- list of transports that may be used by SockJS. By default, all available transports will be used.
For example, the following would create a client with a whitelist of three transport protocols:
final uri = Uri.parse('ws://example.org/echo');
final options = new SockJSOptions(
transports: ['websocket', 'xhr-streaming', 'xhr-polling']);
final client = new SockJSClient(uri, options: options);
Implementation
SockJSClient(Uri uri, {SockJSOptions? options}) {
try {
_jsClient = js_interop.SockJS(uri.toString(), null, options?._toJs());
// ignore: avoid_catches_without_on_clauses
} catch (e) {
if (!js_interop.hasSockJS) {
throw MissingSockJSLibError();
} else {
rethrow;
}
}
manageStreamController(_onCloseController);
manageStreamController(_onMessageController);
manageStreamController(_onOpenController);
_addManagedEventListenerToJSClient('close', _onClose);
_addManagedEventListenerToJSClient('message', _onMessage);
_addManagedEventListenerToJSClient('open', _onOpen);
// Automatically dispose if this client closes. If this close event is
// emitted in response to a call to dispose(), then this will effectively be
// a no-op.
listenToStream<SockJSCloseEvent>(onClose, (_) => dispose());
}