问题
I'm subscribing to only one of many publishers, one at a time (video monitor). So, I initially open all subscribers and when the onconnected event is fired, I store each of them in a hash table (stream.connection.data holds ID). I subscribe to the first, then unsubscribe and subscribe to the next. However, I've had to kill the onconnected event handler so they didn't keep firing multiple times: Tokbox streamCreated being called same number of times client is called
Now, when a new participant joins the session late (after the event handler has been removed) I don't know how to subscribe to their stream since no event fires. I know they have connected and are streaming because I get a message from their application that contains the ID of their stream.
So, I need a way to iterate through all the steams in the session, find the new one, and subscribe to it when it's that person's turn. How, can I get all the streams in a session and look at their connection ID's?
回答1:
TokBox Developer Evangelist here.
To know the number of streams in a session on the client side, I recommend listening to the following session events:
streamCreated
- fires when someone starts publishing in a sessionstreamDestroyed
- fires when someone stops publishing in a session.
I would create an object like below to keep a record of all streams in a session:
const streams = {};
session.on({
streamCreated: event => {
streams[event.stream.streamId] = event.stream;
},
streamDestroyed: event => {
delete streams[event.stream.streamId];
},
};
This would then allow you to access the stream object and subscribe like so:
const stream = streams['f39c6-ae02-100c-9727-b3bf2']; // please note that this is a random stream Id
const subscriber = session.subscribe(stream);
If you would like to know the number of streams in a session on the server side, you can use Session Monitoring and listen to the same events.
来源:https://stackoverflow.com/questions/53947066/opentok-how-enumerate-streams-in-a-session