websocketRoomUrl function

Uri websocketRoomUrl({
  1. required String roomName,
  2. String? baseUrl,
})

Construct the WebSocket URL for a room.

Implementation

Uri websocketRoomUrl({required String roomName, String? baseUrl}) {
  // If no `baseUrl` provided, derive from environment.
  baseUrl ??= String.fromEnvironment('MESHAGENT_API_URL', defaultValue: "");

  if (baseUrl.isEmpty) {
    // Default if not set:
    baseUrl = 'wss://api.meshagent.com';
  } else {
    // Convert http/https to ws/wss if needed:
    if (baseUrl.startsWith('https:')) {
      baseUrl = 'wss:${baseUrl.substring('https:'.length)}';
    } else if (baseUrl.startsWith('http:')) {
      baseUrl = 'ws:${baseUrl.substring('http:'.length)}';
    }
  }

  return Uri.parse('$baseUrl/rooms/$roomName');
}