Skip to content

getResourceUrl()

Description

getResourceUrl() is a method provided by JBLWebSocket, used to retrieve the current best resource URL list for a specific channel (channel) previously connected through connect(key, channel). It returns a prioritized list of resource URLs based on system analysis, sorted from the best to the least optimal.

If no channel was specified in connect, the method will return the resource analysis results for all channels.

⚠️ Important Notes

  1. Requires Prior connect Call

    • You must call the connect method before invoking getResourceUrl. Otherwise, resource information may not be available.
  2. Multi-Channel Return Format

    • If no channel is specified in connect, the result will be an array containing resource lists for multiple channels, each with its own resources list.
  3. Dynamic Resource Switching

    • Based on the returned resource list, you should implement logic to dynamically switch between resources. For example, if the current resource fails during playback, you can switch to the next resource in the priority list.

Syntax

javascript
const resourceUrlObj = JBLWebSocket.getResourceUrl();

Return Value

  • Type: Object
  • Description: Returns a structured object containing channel names and their respective resource URL lists.

Return Format

javascript
{
    result: [
        {
            "resources": [
                "cdn1-mcn2.xcssmkj.com",  // Best resource
                "cdn1-mx2.st6689.com",    // Second best resource
                "cdn3-mcn2.xcssmkj.com",  // Less optimal resource
                "cdn3-mx2-s.ctyisheng.com" // Least optimal resource
            ],
            "name": "MY"  // Channel name
        },
        // If no channel was specified in connect, results may include multiple channels
        {
            "resources": [
                "cdn4-abc.xcssmkj.com",
                "cdn2-def.st6689.com",
                "cdn5-ghi.xcssmkj.com"
            ],
            "name": "JP"  // Another channel name
        }
    ]
}

Features and Use Cases

  1. Dynamic Selection of the Best Resource

    • The method provides prioritized resource URLs based on system analysis, allowing the player to automatically choose the best resource for playback.
  2. Multi-Channel Support

    • If no channel is specified, the method returns resource information for all channels, making it suitable for managing resources in a multi-channel environment.
  3. Efficient Resource Utilization

    • The returned list is sorted by priority, enabling dynamic switching to the optimal resource and improving the playback experience.

Examples

1. Retrieve the Best Resource for a Specific Channel

Suppose the connect method was previously used to connect to the channel MY. You can retrieve the resource information for that channel as follows:

javascript
JBLWebSocket.connect('yourKey', 'MY'); // Connect to a specific channel

const resourceUrlObj = JBLWebSocket.getResourceUrl(); // Retrieve resource information
console.log(resourceUrlObj);

// Parse the best resource
const bestResource = resourceUrlObj.result[0]?.resources[0]; // Best resource
if (bestResource) {
    console.log(`The best resource is: ${bestResource}`);
} else {
    console.error('Failed to retrieve the best resource');
}

2. Retrieve Resource Information for All Channels

If no channel was specified in connect, the method returns the resource lists for all channels:

javascript
JBLWebSocket.connect('yourKey'); // Connect without specifying a channel

const resourceUrlObj = JBLWebSocket.getResourceUrl();
console.log(resourceUrlObj);

// Parse the best resource for each channel
resourceUrlObj.result.forEach(channel => {
    console.log(`The best resource for channel ${channel.name} is: ${channel.resources[0]}`);
});

Common Use Cases

  • In live or on-demand streaming environments, dynamically selecting the best resource based on real-time performance to reduce buffering or interruptions.
  • Supporting multi-channel environments by allowing backend systems to allocate and switch resources dynamically for different channels.

Through JBLWebSocket.getResourceUrl(), developers can efficiently retrieve channel resource information and implement automated selection and dynamic switching of the best resources, improving overall playback stability and user experience.