我正在为多用户使用 webRTC 应用程序的网格架构,视频聊天适用于多用户,对于文本聊天,我在创建报价的对等方上创建了一个 dataChannel,而 onDataChannel 处理程序在另一个对等方上创建了一个 dataChannel。文本聊天适用于 2 个用户,但是当有 3 个用户时,第一个客户端(加入)能够看到所有其他客户端消息,但只能向第二个客户端(加入)发送消息,第二个客户端(加入)是能够看到来自第一个客户端的消息并仅发送给第一个客户端,第三个客户端没有收到消息但可以发送给第一个客户端。
PeerConnection.Observer 中的 onDataChannel 事件处理程序
@Override
public void onDataChannel(DataChannel dataChannel) {
Log.d("DataChannel", "onDataChannel" + " , state: " + dataChannel.state());
DataChannel.Observer dcObserver = new DcObserver(){
@Override
public void onStateChange() {
Log.d(TAG, "onStateChange: remote data channel state: " + dChannel.state().toString());
}
@Override
public void onMessage(DataChannel.Buffer buffer) {
Log.d(TAG, "onMessage: got message");
readMessage(buffer.data);
}
};
dataChannel.registerObserver(dcObserver);
dataChannels.add(dataChannel);
dcObservers.add(dcObserver);
}
});
在创建 peerConnections 时创建 DataChannel
@Override
public void onNewPeerJoined(String socketId, boolean createOffer) {
showToast("Remote Peer Joined");
PeerConnection peerConnection = getOrCreatePeerConnection(socketId);
if (createOffer) {
dChannel = peerConnection.createDataChannel("DataChannel",new DataChannel.Init());
DataChannel.Observer dcObserver = new DcObserver(){
@Override
public void onStateChange() {
Log.d(TAG, "onStateChange: remote data channel state: " + dChannel.state().toString());
}
@Override
public void onMessage(DataChannel.Buffer buffer) {
Log.d(TAG, "onMessage: got message");
readMessage(buffer.data);
}
};
dChannel.registerObserver(dcObserver);
dcObservers.add(dcObserver);
dataChannels.add(dChannel);
sdpConstraints = new MediaConstraints();
sdpConstraints.mandatory.add(
new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
sdpConstraints.mandatory.add(
new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"));
//sdpConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
peerConnection.createOffer(new CustomSdpObserver("localCreateOffer") {
@Override
public void onCreateSuccess(SessionDescription sessionDescription) {
super.onCreateSuccess(sessionDescription);
peerConnection.setLocalDescription(new CustomSdpObserver("localSetLocalDesc"), sessionDescription);
Log.d("onCreateSuccess", "SignallingClient emit ");
SignallingClient.getInstance(roomName).emitSessionDescription(sessionDescription, socketId);
}
}, sdpConstraints);
}
}
发送消息
public void sendMessage() {
String message = editText.getText().toString();
if (message.isEmpty()) {
return;
}
editText.setText("");
textView.append(HtmlCompat.fromHtml("<b>" + userName + "</b>",HtmlCompat.FROM_HTML_MODE_LEGACY));
textView.append(": " + message + "\n");
String str = "<b>" + userName + "</b> : " + message;
ByteBuffer data = stringToByteBuffer("-s" + str, Charset.defaultCharset());
for (int i = 0; i < dataChannels.size(); i++){
Log.d("info","send_data dataChannel" + i);
dataChannels.get(i).send(new DataChannel.Buffer(data, false));
}
}