1
我試圖在Android上實現數據通道。我使用數據通道將數據從android成功發送到web。WebRTC onMessage()不會在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對象創建對象。我有這個包含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類的外觀。
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);
但我不知道是什麼。
看起來不錯給我。唯一不尋常的是你在UI線程上註冊觀察者的事實。你爲什麼做這個?你可以嘗試直接在'onDataChannel'方法中使用它。 –
如果我不在UI線程上運行它,那麼我的應用程序崩潰。也許,很多工作正在發生。我找到了解決方案,並會爲此發佈答案。 –