WebRTC onMessage() 不会在 Android 上触发
WebRTC onMessage() does not trigger on Android
我已尝试在 Android 上实施数据通道。我使用数据通道成功地将数据从 android 发送到网络。
但是,每当我尝试将数据从网络发送到 android 时,android 都收不到数据并且永远不会触发 onMessage()。
我不明白为什么会这样。这是我如何实现 DataChannel.Observer.
private class DcObserver implements DataChannel.Observer {
// removed other overrided messages for clarity
@Override
public void onMessage(DataChannel.Buffer buffer) {
Toast.makeText(getApplicationContext(),
"Some Data has been received", Toast.LENGTH_SHORT)
.show();
Log.d("Data", "Some Data has been received");
}
}
我为每个 PeerConnection 对象创建对象。我有这个 class,其中包含 PeerConnection 对象。
private class FilePeer implements SdpObserver {
private PeerConnection pc;
private DataChannel dc;
// removed other overrided methods for clarity
public FilePeer() {
PcObserver pcObserver = new PcObserver();
pc = factory.createPeerConnection(RTCConfig.getIceServer(),
RTCConfig.getMediaConstraints(), pcObserver);
dc = pc.createDataChannel("sendDataChannel", new DataChannel.Init());
//DcObserver dcObserver = new DcObserver();
//dc.registerObserver(dcObserver); // This one crashes the app, therefore, getting the datachannel using onDataChannel
}
}
这就是 PcObserver class 的样子。
private class PcObserver implements PeerConnection.Observer{
@Override
public void onDataChannel(final DataChannel dataChannel) {
runOnUiThread(new Runnable() {
public void run() {
peer.dc = dataChannel;
DcObserver dcObserver = new DcObserver();
peer.dc.registerObserver(dcObserver);
//dataChannel.registerObserver(dcObserver);
}
});
}
// other methods are removed for clarity
}
我觉得我做错了 peer.dc.registerObserver(dcObserver);
但我不知道是什么。
我在 webrtc 组上找到了这个问题的解决方案。
我需要保留观察者的引用,以确保它不会在 DataChannel 回调时被销毁。
还有一件事,我们需要做的是删除 onDataChannel();
、
中的 peer.dc = dataChannel;
以下代码现在对我来说工作正常:
public void onDataChannel(final DataChannel dataChannel) {
final DataChannel dc = dataChannel;
runOnUiThread(new Runnable() {
public void run() {
DcObserver dcObserver = new DcObserver();
peer.dc.registerObserver(dcObserver);
}
});
}
我已尝试在 Android 上实施数据通道。我使用数据通道成功地将数据从 android 发送到网络。
但是,每当我尝试将数据从网络发送到 android 时,android 都收不到数据并且永远不会触发 onMessage()。
我不明白为什么会这样。这是我如何实现 DataChannel.Observer.
private class DcObserver implements DataChannel.Observer {
// removed other overrided messages for clarity
@Override
public void onMessage(DataChannel.Buffer buffer) {
Toast.makeText(getApplicationContext(),
"Some Data has been received", Toast.LENGTH_SHORT)
.show();
Log.d("Data", "Some Data has been received");
}
}
我为每个 PeerConnection 对象创建对象。我有这个 class,其中包含 PeerConnection 对象。
private class FilePeer implements SdpObserver {
private PeerConnection pc;
private DataChannel dc;
// removed other overrided methods for clarity
public FilePeer() {
PcObserver pcObserver = new PcObserver();
pc = factory.createPeerConnection(RTCConfig.getIceServer(),
RTCConfig.getMediaConstraints(), pcObserver);
dc = pc.createDataChannel("sendDataChannel", new DataChannel.Init());
//DcObserver dcObserver = new DcObserver();
//dc.registerObserver(dcObserver); // This one crashes the app, therefore, getting the datachannel using onDataChannel
}
}
这就是 PcObserver class 的样子。
private class PcObserver implements PeerConnection.Observer{
@Override
public void onDataChannel(final DataChannel dataChannel) {
runOnUiThread(new Runnable() {
public void run() {
peer.dc = dataChannel;
DcObserver dcObserver = new DcObserver();
peer.dc.registerObserver(dcObserver);
//dataChannel.registerObserver(dcObserver);
}
});
}
// other methods are removed for clarity
}
我觉得我做错了 peer.dc.registerObserver(dcObserver);
但我不知道是什么。
我在 webrtc 组上找到了这个问题的解决方案。
我需要保留观察者的引用,以确保它不会在 DataChannel 回调时被销毁。
还有一件事,我们需要做的是删除 onDataChannel();
、
peer.dc = dataChannel;
以下代码现在对我来说工作正常:
public void onDataChannel(final DataChannel dataChannel) {
final DataChannel dc = dataChannel;
runOnUiThread(new Runnable() {
public void run() {
DcObserver dcObserver = new DcObserver();
peer.dc.registerObserver(dcObserver);
}
});
}