Skip to main content

[WebService] Tokbox, Opentok Network Test

Opentok-network-test @ Github

create otNetworkTest object#

// createimport OT from '@opentok/client';import NetworkTest, { ErrorNames } from 'opentok-network-test-js';
const startNetWorkTest = async ({ timeoutSeconds = 10 }) => {  try {    const sessionInfo = {      apiKey: '123456', // Add the API key for your OpenTok project here.      sessionId: '1_MX40NzIwMzJ-fjE1MDElGQkJJfn4', // Add a test session ID for that project      token: 'T1==cGFydG5lcXN0PQ==', // Add a token for that session here    };    const options = {      timeout: timeoutSeconds * 1000, // 5000 (5 seconds) but less than 30000 (30 seconds)    };    const otNetworkTest = new NetworkTest(OT, sessionInfo, options);  } catch (error) {    console.error('[PreCallCheck] startNetWorkTest failed: ', error);  }};

testConnectivity#

回傳物件#

// connectivityResult: success{  success: true,  failedTests: [],};
// connectivityResult: failed{  success: false,  failedTests: [    {      error: Error Object,      type: 'OpenTok.js',    },  ],};

呼叫方法#

import NetworkTest, { ErrorNames } from 'opentok-network-test-js';const startConnectivityTest = async (otNetworkTest) => {  try {    const connectivityResult = await otNetworkTest.testConnectivity();
    if (!connectivityResult.success && connectivityResult.failedTests) {      const firstFailedTest = connectivityResult.failedTests[0];      const { error, type } = firstFailedTest;      const { name, message } = error;
      switch (name) {        case ErrorNames.API_CONNECTIVITY_ERROR:        case ErrorNames.CONNECT_TO_SESSION_ERROR:        case ErrorNames.CONNECT_TO_SESSION_TOKEN_ERROR:        case ErrorNames.CONNECT_TO_SESSION_ID_ERROR:        case ErrorNames.CONNECT_TO_SESSION_NETWORK_ERROR:        case ErrorNames.PUBLISH_TO_SESSION_ERROR:        case ErrorNames.FAILED_MESSAGING_SERVER_TEST:        case ErrorNames.PUBLISH_TO_SESSION_NOT_CONNECTED:        case ErrorNames.PUBLISH_TO_SESSION_PERMISSION_OR_TIMEOUT_ERROR:        case ErrorNames.PUBLISH_TO_SESSION_NETWORK_ERROR:        case ErrorNames.SUBSCRIBE_TO_SESSION_ERROR:        case ErrorNames.LOGGING_SERVER_CONNECTION_ERROR:          console.error('API Connectivity Error');          break;        case ErrorNames.FAILED_TO_CREATE_LOCAL_PUBLISHER:        case ErrorNames.FAILED_TO_OBTAIN_MEDIA_DEVICES:          console.error('Please granting access to the microphone and camera...');          break;        case ErrorNames.NO_AUDIO_CAPTURE_DEVICES:        case ErrorNames.NO_VIDEO_CAPTURE_DEVICES:          console.error('No available camera or microphone...');          break;        default:          console.error('[PreCallCheck] startConnectivityTest failed: ', error);      }    }
    return connectivityResult.success;  } catch (error) {    console.error('[PreCallCheck] startConnectivityTest failed: ', error);  }};

testQuality#

import NetworkTest, { ErrorNames } from 'opentok-network-test-js';
const startQualityTest = async ({ otNetworkTest, timeoutSeconds }) => {  try {    let i = 0;    const qualityResult = await otNetworkTest.testQuality((stats) => {      i++;      const raw = i / timeoutSeconds;      const percentage = raw > 1 ? 100 : Math.floor(raw * 100);      console.log('testQuality stats', stats, percentage);    });    return qualityResult;  } catch (error) {    switch (error.name) {      case ErrorNames.UNSUPPORTED_BROWSER:        console.error('You browser is not supported');        break;      case ErrorNames.CONNECT_TO_SESSION_ERROR:      case ErrorNames.CONNECT_TO_SESSION_NETWORK_ERROR:        console.error('Please check your network connectivity...');        break;      case ErrorNames.FAILED_TO_OBTAIN_MEDIA_DEVICES:        console.error('Please grant access to the microphone and camera...');        break;      case ErrorNames.NO_AUDIO_CAPTURE_DEVICES:      case ErrorNames.NO_VIDEO_CAPTURE_DEVICES:        console.error('No available camera or microphone...');        break;      case ErrorNames.CONNECT_TO_SESSION_TOKEN_ERROR:      case ErrorNames.CONNECT_TO_SESSION_ID_ERROR:      case ErrorNames.PUBLISH_TO_SESSION_ERROR:      case ErrorNames.INIT_PUBLISHER_ERROR:      case ErrorNames.PUBLISH_TO_SESSION_NOT_CONNECTED:      case ErrorNames.PUBLISH_TO_SESSION_PERMISSION_OR_TIMEOUT_ERROR:      case ErrorNames.SUBSCRIBE_TO_SESSION_ERROR:      case ErrorNames.SUBSCRIBER_GET_STATS_ERROR:        console.error('[PreCallCheck] startQualityTest failed: ', error);        break;      default:        console.error('[PreCallCheck] startQualityTest failed: ', error);    }  }};

quality 回傳結果物件#

const qualityResult = {  audio: {    bitrate: 37262.4,    packetLossRatio: 0,    supported: true,    mos: 4.4296671352,    reason: '',  },  video: {    bitrate: 434432,    packetLossRatio: 0,    supported: true,    mos: 3.462114754796132,    frameRate: 30,    recommendedResolution: '320x240',    recommendedFrameRate: 30,    reason: '',  },};