用信号通知OpenTok和React

Hir*_*ard 2 javascript tokbox opentok reactjs

有没有人用opentok-react https://github.com/aiham/opentok-react实现发送和接收信号?我什至找不到关于如何使用opentok-react在React中进行操作的简单示例。

aih*_*ham 6

感谢您使用opentok-react。不幸的是,尚未在opentok-react中添加执行信令的简单方法,因此以下过程有些麻烦。

要进行信号发送,您将需要像平常一样访问Session对象并在其上调用signal方法(请参阅https://tokbox.com/developer/sdks/js/reference/Session.html#signal)。

如果使用了OTSession组件,则可以通过获取OTSession元素的引用来访问Session对象(请参阅https://reactjs.org/docs/refs-and-the-dom.html)。

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.otSession = React.createRef();
  }
  render() {
    return <OTSession ref={this.otSession} />;
  }
}
Run Code Online (Sandbox Code Playgroud)

并使用其sessionHelper属性调用signal方法:

this.otSession.current.sessionHelper.session.signal(...);
Run Code Online (Sandbox Code Playgroud)

如果要为收件人指定特定的目标连接,则需要从基础Publisher或Subscriber对象的stream属性中获取它。首先获取对OTPublisher或OTSubscriber元素的引用:

<OTPublisher ref={this.otPublisher} />
// or
<OTSubscriber ref={this.otSubscriber} />
Run Code Online (Sandbox Code Playgroud)

然后访问Connection对象:

this.otPublisher.current.getPublisher().stream.connection
// or
this.otSubscriber.current.getSubscriber().stream.connection
Run Code Online (Sandbox Code Playgroud)

我尚未对此进行测试,但是一旦您可以访问会话和连接对象,便可以使用OpenTok JS SDK的全部信号功能。