有没有人用 opentok-react 实现发送和接收信号 https://github.com/aiham/opentok-react ?我什至找不到关于如何使用 opentok-react 在 React 中执行此操作的简单示例。
请您参考如下方法:
感谢使用 opentok-react。不幸的是,opentok-react 尚未添加一种简单的信号发送方法,因此以下过程有点复杂。
要发送信号,您需要访问 Session 对象并像往常一样调用它的信号方法(参见 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} />;
}
}
并使用其 sessionHelper 属性调用信号方法:
this.otSession.current.sessionHelper.session.signal(...);
如果您想为接收者指定一个特定的目标连接,那么您需要从底层发布者或订阅者对象的流属性中获取它。首先获取对 OTPublisher 或 OTSubscriber 元素的引用:
<OTPublisher ref={this.otPublisher} />
// or
<OTSubscriber ref={this.otSubscriber} />
然后访问连接对象:
this.otPublisher.current.getPublisher().stream.connection
// or
this.otSubscriber.current.getSubscriber().stream.connection
我没有对此进行测试,但是一旦您有权访问 Session 和 Connection 对象,您就可以使用 OpenTok JS SDK 的完整信令功能。