打开Tok多方视频会议问题

时间:2014-07-03 05:02:46

标签: ruby-on-rails-3 opentok

以下是我创建会话的代码。令牌作为服务器端。我正在使用Ruby SDK。

opentok = OpenTok::OpenTok.new(ENV['TOKBOX_API_KEY'], ENV['TOKBOX_SECRET_KEY'])
location = 'my ipaddress'
session = opentok.create_session( {:media_mode => :routed, :location => location, 'p2p.preference' => "disabled"} )
token = opentok.generate_token(session_id, {:expire_time => meeting.date.to_i+(1 * 24 * 60 * 60), :data => user.full_name})

所以每次我只为一个房间创建一个会话和多个令牌。

当我的第一个用户加入房间时,它工作正常,当第二个用户加入房间时,它也正常工作。

问题从这里开始

但是当我的第三个用户加入具有相同会话ID的房间时,每次用户只能看到2个用户时,目前在我的房间中有3个用户在场,但只有2个用户可见。

这是JavaScript代码

 var apiKey = "<%= ENV['TOKBOX_API_KEY']%>";

  TB.addEventListener("exception", exceptionHandler);
  var session = TB.initSession("<%= @room.session_id %>");
  session.addEventListener("sessionConnected", sessionConnectedHandler);
  session.addEventListener("streamCreated", streamCreatedHandler);
  session.addEventListener("connectionDestroyed", connectionDestroyedHandler);
  session.connect(apiKey, "<%= @room.token %>");

  function sessionConnectedHandler(event) {
     subscribeToStreams(event.streams);
     publisher = TB.initPublisher(apiKey, "publisher_replace", {position: 'absolute',
      width: 350, height: 200, wmode: "window"});
      session.publish(publisher);
      publisher.publishAudio(true);



  }

  function streamCreatedHandler(event) {
    subscribeToStreams(event.streams);
  }

  function subscribeToStreams(streams) {
    for (var i = 0; i < streams.length; i++) {
      var stream = streams[i];

      if (stream.connection.connectionId != session.connection.connectionId) {
        var newDiv = document.createElement("div");
        newDiv.id = "subscriber_replace";
        document.getElementById("subscriber_container").appendChild(newDiv);
        subscriber = session.subscribe(stream, "subscriber_replace", {width: 350, height: 200});
        subscriber.subscribeToAudio(true);
        subscriber.setAudioVolume(100);
        //alert(session.connection.data + " " + " has joined the room");
      }
    } 

这是我的Html代码

<div id="chatBox">

          <div id="publisher_container">
              <div id="publisher_replace"></div>
          </div>

          <div id="subscriber_container">
            <div id="subscriber_replace" ></div>
          </div>
 </div>

请帮帮我

提前致谢!

1 个答案:

答案 0 :(得分:2)

问题出在您的订阅流方法中。您正在创建一个id为&#34; subscriber_replace&#34;的新div元素。当第三个人加入你的房间时,第二个用户div(id为#34; subscriber_replace&#34;)被第三个流替换。这里有一些代码修改的重写(streamCreated事件只提供1个流)。

function streamCreatedHandler(event) {
  var newDiv = document.createElement("div");
  newDiv.id = "subscriber_replace" + event.stream.streamId; // stream will have its own id
  document.getElementById("subscriber_container").appendChild(newDiv);
  subscriber = session.subscribe(event.stream, newDiv.id, {width: 350, height: 200});
  ...
}