我可以从 ref OTSession 访问事件处理程序吗
Can I access event handlers from ref OTSession
我正在尝试通过 streamCreated
、signal
、streamDestroyed
等引用访问来自 OTSession 的事件。那样可能吗:
How I think it should work
来自 TokBox 的 Manik。
您无法通过 ref 访问事件,因为我们将事件传递给 native 层并在挂载组件时设置了监听器,但是,您可以像这样监听事件:
import React, { Component } from 'react';
import { View } from 'react-native';
import { OTSession, OTPublisher, OTSubscriber } from 'opentok-react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.apiKey = '';
this.sessionId = '';
this.token = '';
this.sessionEventHandlers = {
streamCreated: event => {
console.log("stream created", event);
},
streamDestroyed: event => {
console.log("stream destroyed", event);
},
};
}
render() {
return (
<View style={{ flex: 1, flexDirection: 'row' }}>
<OTSession apiKey={this.apiKey} sessionId={this.sessionId} token={this.token} eventHandlers={this.sessionEventHandlers}>
<OTPublisher style={{ width: 100, height: 100 }} />
<OTSubscriber style={{ width: 100, height: 100 }} />
</OTSession>
</View>
);
}
}
我正在尝试通过 streamCreated
、signal
、streamDestroyed
等引用访问来自 OTSession 的事件。那样可能吗:
How I think it should work
来自 TokBox 的 Manik。
您无法通过 ref 访问事件,因为我们将事件传递给 native 层并在挂载组件时设置了监听器,但是,您可以像这样监听事件:
import React, { Component } from 'react';
import { View } from 'react-native';
import { OTSession, OTPublisher, OTSubscriber } from 'opentok-react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.apiKey = '';
this.sessionId = '';
this.token = '';
this.sessionEventHandlers = {
streamCreated: event => {
console.log("stream created", event);
},
streamDestroyed: event => {
console.log("stream destroyed", event);
},
};
}
render() {
return (
<View style={{ flex: 1, flexDirection: 'row' }}>
<OTSession apiKey={this.apiKey} sessionId={this.sessionId} token={this.token} eventHandlers={this.sessionEventHandlers}>
<OTPublisher style={{ width: 100, height: 100 }} />
<OTSubscriber style={{ width: 100, height: 100 }} />
</OTSession>
</View>
);
}
}