【问题标题】:Twilio video SDK and access token in plain javascript - documentation?纯javascript中的Twilio视频SDK和访问令牌-文档?
【发布时间】:2020-10-21 02:26:50
【问题描述】:

我正在尝试使用 JS 但不使用节点来制作视频应用程序。据我了解,我需要做的第一件事是创建一个 JWT 令牌。

根据以下文档 (https://media.twiliocdn.com/sdk/js/video/releases/2.7.2/docs/) 如果我使用 CDN 文件,我会通过这样做来初始化我的 JS

const Video = Twilio.Video;

而不是

const Video = require('twilio-video');

但是,要获得 JWT 令牌,我似乎需要加载另一个 CDN 文件? (似乎还有一个 Twilio 助手 JS 可能 (https://www.twilio.com/docs/voice/client/javascript/device#method-reference))因为 twilio 的示例用于获取令牌加载不同的库,但我无法在任何地方找到这个 JS 文件

我的问题是,我可以使用 CDN 文件生成 jwt 令牌吗?在他们的文档中,还是我也需要加载不同的文件?

<script src="//media.twiliocdn.com/sdk/js/video/releases/2.7.2/twilio-video.min.js"></script>

以及如何在普通 js 中初始化 const AccessToken 而不是(节点版本)

const AccessToken = require('twilio').jwt.AccessToken;

【问题讨论】:

  • 如果要求让应用只运行 JavaScript 运行时,为什么不使用 nodeJs 编写脚本,然后使用 browserify 捆绑它,以便它可以在浏览器/JavaScript 运行时运行?
  • @AyushVipul,不,这不是要求
  • 在我之前的所有用例中,我们所做的是使用 Rest API 生成 JWT 令牌,效果很好。你可以试试看。
  • @BilalMehrban 你有一个例子说明你是如何做到的,正如我在问题中所说,我的印象是我需要链接另一个不在文档中的 JS 文件

标签: twilio-api twilio-video twilio-javascript


【解决方案1】:

@Manza,正如你在 cmets 中所问的,我正在分享我所做的示例代码。

第 1 步:使用 ajax 发送一个帖子请求,并将名称和会议 PIN 作为用户的输入。

第 2 步:在 Asp.Net MVC 4.7 中使用 REST API 根据会议 Pin 图生成令牌

public JsonResult GenerateToken(string userName,int pin)
    {
      int ExpiryDuration = 120;
      var grants = new HashSet<IGrant>();
      var videoGrant = new VideoGrant();
      var roomName = Guid.NewGuid().ToString();
      videoGrant.Room = roomName;
      grants.Add(videoGrant);
      DateTime expireTime = DateTime.Now.AddMinutes(ExpiryDuration);
      var token = new Token(
                twilioAccountAccountSid,
                twilioAccountApiKey,
                twilioAccountApiSecret,
                userName,
                grants: grants,
                expiration: expireTime).ToJwt();
     return Json(new { token , roomName  }, JsonRequestBehavior.AllowGet);
    }

第 3 步:在 ajax 成功的前端,我将令牌保存在 localstorage 中并重定向到一个新页面,其中包含此 JS SDK 以从客户端完成所有工作

 <script src="//media.twiliocdn.com/sdk/js/video/releases/2.7.2/twilio-video.min.js"></script>

第 4 步:使用我使用以下代码的令牌创建新连接

const ID_TOKEN = "BEARER_DETAILS";
// ConnectOptions settings for a video web application.
const connectOptions = {
    // Available only in Small Group or Group Rooms only. Please set "Room Type"
    // to "Group" or "Small Group" in your Twilio Console:
    // https://www.twilio.com/console/video/configure
    bandwidthProfile: {
        video: {
            mode: 'collaboration',
            trackSwitchOffMode: 'predicted',
            dominantSpeakerPriority: 'high',
            maxTracks: 3,
            renderDimensions: {
                high: { width: 1080, height: 720 },
                standard: { width: 640, height: 480 },
                low: { width: 320, height: 240 }
            }
        }
    },
    networkQuality: {
        local: 1, // LocalParticipant's Network Quality verbosity [1 - 3]
        remote: 2 // RemoteParticipants' Network Quality verbosity [0 - 3]
    },


    // Available only in Small Group or Group Rooms only. Please set "Room Type"
    // to "Group" or "Small Group" in your Twilio Console:
    // https://www.twilio.com/console/video/configure
    dominantSpeaker: true,
    // Comment this line to disable verbose logging.
    logLevel: 'debug',

    // Comment this line if you are playing music.
    maxAudioBitrate: 16000,

    // VP8 simulcast enables the media server in a Small Group or Group Room
    // to adapt your encoded video quality for each RemoteParticipant based on
    // their individual bandwidth constraints. This has no utility if you are
    // using Peer-to-Peer Rooms, so you can comment this line.
    preferredVideoCodecs: [{ codec: 'VP8', simulcast: true }],

    // Capture 720p video @ 24 fps.
    video: { height: 720, frameRate: 24, width: 1280 }
};

// For mobile browsers, limit the maximum incoming video bitrate to 2.5 Mbps.
if (isMobile) {
    connectOptions
        .bandwidthProfile
        .video
        .maxSubscriptionBitrate = 2500000;
}
data = JSON.parse(window.localStorage.getItem(ID_TOKEN));
const token = await data.Token;
connectOptions.name = data.RoomName;
// Join to the Room with the given AccessToken and ConnectOptions.
const room = await Twilio.Video.connect(token, connectOptions);

我确实尝试了尽可能多的内容,如果有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多