rails 应用中的 Opentok 错误,"OT is not defined"

Opentok error in rails app, "OT is not defined"

我在 rails 应用上的 ruby 中收到错误 "OT is not defined" 错误:

API 密钥、session_id 和令牌具有正确的值。

我在我的 gemfile 中包含 "gem 'opentox'" 和 运行 'bundle install'。 不确定如何初始化 OT。

Show.html.erb:

<script type="text/javascript">
  var apiKey = '<%= @api_key %>';
  var sessionId = '<%= @room.session_id %>';
  var token = '<%= @token %>';
</script>

<div id="videos">
  <div id="publisher"></div>
  <div id="subscribers"></div>
</div>

<script>
  // Handling all of our errors here by alerting them
  function handleError(error) {
    if (error) {
       alert(error.message);
       }
    }

    initializeSession();

    function initializeSession() {
      var session = OT.initSession(apiKey, sessionId);

      // Subscribe to a newly created stream
      session.on('streamCreated', function(event) {
      session.subscribe(event.stream, 'subscriber', {
        insertMode: 'append',
        width: '100%',
        height: '100%'
      }, handleError);
      });

     // Create a publisher
    var publisher = OT.initPublisher('publisher', {
      insertMode: 'append',
      width: '100%',
      height: '100%'
    }, handleError);

   // Connect to the session
   session.connect(token, function(error) {
   // If the connection is successful, publish to the session
   if (error) {
      handleError(error);
    } else {
    session.publish(publisher, handleError);
    }
    });
   }
  </script>

通过将 opentok 脚本标记添加到 show.html.erb,我能够解决这个问题: <script src="https://static.opentok.com/v2/js/opentok.min.js"></script>

OT 是 opentok 的对象,用于初始化会话、销毁会话等。如果您必须使用 OT 对象,则必须在 application.html.erb 中使用 cdn,以便可以在中使用该 OT 对象每一页 你只需要在 application.html.erb

的 head 标签中放置一个 cdn

<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>