connectLocalSocket function
- ConnectLocalSocketOptions options
Connects to a media socket with the specified options.
Returns a ResponseLocalConnection
containing the socket and connection data.
Throws an exception if inputs are invalid or if connection fails.
Example usage:
final options = ConnectLocalSocketOptions(
link: "http://localhost:3000",
);
try {
final response = await connectLocalSocket(options);
print("Connected to local socket with ID: ${response.data.socketId}");
} catch (error) {
print("Failed to connect to local socket: $error");
}
Connects to a local media socket with the specified options.
Returns a ResponseLocalConnection
containing the socket and connection data.
Implementation
/// Connects to a local media socket with the specified options.
/// Returns a `ResponseLocalConnection` containing the socket and connection data.
Future<ResponseLocalConnection> connectLocalSocket(
ConnectLocalSocketOptions options) async {
if (options.link.isEmpty) throw Exception('Socket link required.');
final socket = io.io('${options.link}/media', <String, dynamic>{
'transports': ['websocket'],
});
// final socket = io.io('${options.link}/media', {
// 'transports': ['websocket'],
// 'query': {},
// });
final completer = Completer<ResponseLocalConnection>();
// Handle connection success
socket.on('connection-success', (data) {
final connectionData =
ResponseLocalConnectionData.fromMap(Map<String, dynamic>.from(data));
completer.complete(
ResponseLocalConnection(socket: socket, data: connectionData));
});
// Handle connection error
socket.onConnectError((error) {
completer.completeError(
Exception('Error connecting to local media socket: $error'));
});
return completer.future;
}