What is the proper procedure to do ICE restart when using SIP.js? (v0.20.0)
This is what I'm trying:
oniceconnectionstatechange: (event) => {
const newState = sdh.peerConnection.iceConnectionState;
if (newState == 'failed') {
sdh.peerConnection.restartIce();
sdh.peerConnection.createOffer({'iceRestart': true})
.then(function(offer) {
return sdh.peerConnection.setLocalDescription(offer);
});
}
}
It seems to execute without an error, but also no result.
FireFox debug-tool "about:webrtc" shows "ICE restarts: 0", so I guess it didn't even begin restart.
ps: failed state is induced by restarting RTP Engine (Kamailio setup). After RTP Engine restarts there is still audio for about 20 seconds and only when ICE state changes to "failed" audio stops.
Found solution that fixed my problem:
sdh.peerConnectionDelegate = {
oniceconnectionstatechange: (event) => {
const newState = sdh.peerConnection.iceConnectionState;
if (newState === 'disconnected') {
sdh.peerConnection.restartIce();
sdh.peerConnection.createOffer({'iceRestart': true})
.then((offer) => {
sdh.peerConnection.setLocalDescription(offer);
session.sessionDescriptionHandlerModifiersReInvite = [offer];
session.invite()
.then(() => {
session.logger.debug('iceRestart: RE-invite completed');
})
.catch((error) => {
if (error instanceof RequestPendingError) {
session.logger.error('iceRestart: RE-invite is already in progress');
}
throw error;
});
});
}
}
};
Related
I have a tcp socket client connection and server in my app. My main goal is taking data from server and send it to client connection. It works great while my app is running but if i get app to background it just sends one data and sends other datas after opening app again. I tried expo-task-manager but it just sends first data and doesnt sends after it as before. I was using my function inside useEffect in a context component. My function with task manager is below.
const BACKGROUND_CLIENT_TASK = "background-client-task";
TaskManager.defineTask(BACKGROUND_CLIENT_TASK, async () => {
const now = Date.now();
console.log(
`Got background fetch call at date: ${new Date(now).toISOString()}`
);
const mainFunction = async () => {
const server = TcpSocket.createServer(function (socket) {
socket.on("data", (takenData) => {
const jsonStringData = String.fromCharCode(...takenData);
const data = JSON.parse(jsonStringData);
const clientOptions = {
port: 1500,
host: "localhost",
};
const dataToClientArray = [
`DataProcess1${data}`,
`DataProcess2${data}`,
`DataProcess3${data}`,
];
dataToClientArray.forEach((dataToClient, index) => {
setTimeout(() => {
const client = TcpSocket.createConnection(
clientOptions,
() => {
// Write on the socket
client.write(`${dataToClient}`, "hex");
console.log("client started");
console.log(dataToClient);
// Close socket
client.destroy();
}
);
client.on("data", (data) => {
console.log("Received: ", data.toString());
});
client.on("error", (error) => {
console.log("Error: ", error);
});
client.on("close", () => {
console.log("Connection closed");
});
}, 300 * index);
});
});
socket.on("error", (error) => {
console.log("An error ocurred with client socket ", error);
});
socket.on("close", (error) => {
console.log("Closed connection with ", socket.address());
});
}).listen({ port: 1754, host: "0.0.0.0" });
server.on("error", (error) => {
console.log("An error ocurred with the server", error);
});
server.on("close", () => {
console.log("Server closed connection");
});
};
mainFunction();
// Be sure to return the successful result type!
return BackgroundFetch.BackgroundFetchResult.NewData;
});
BTW It doesnt start if i dont add the mainFunction in useEffect
The code that i register my defined Task. (result returns undefined)
async function registerBackgroundFetchAsync() {
return BackgroundFetch.registerTaskAsync(BACKGROUND_CLIENT_TASK, {
minimumInterval: 5, // seconds
stopOnTerminate: false, // android only,
startOnBoot: true, // android only
});
}
const registerFunction = async () => {
const result = await registerBackgroundFetchAsync();
console.log(result);
console.log("resultup");
const status = await BackgroundFetch.getStatusAsync();
const isRegistered = await TaskManager.isTaskRegisteredAsync(
BACKGROUND_CLIENT_TASK
);
setStatus(status);
setIsRegistered(isRegistered);
};
registerFunction();
I went through several SO threads regarding this. Didn't work anything. But my case is different. In my case, nothing happens. Here is the code:
import amqplib from 'amqplib'
const AMQP_URL = 'amqp://guest:guest#localhost:5672/'
const AMQP_QUEUE_NAME = 'email_queue'
function connectRabbitMQ() {
console.log('Connecting to rabbit mq...')
amqplib.connect(AMQP_URL, async (err: Error, connection: amqplib.Connection) => {
if (err) {
console.log('This log doesn\'t print')
throw err
}
console.log('This log doesn\'t print')
// Listener
const channel1 = await connection.createChannel()
channel1.consume(AMQP_QUEUE_NAME, (msg) => {
if (msg !== null) {
console.log('Recieved:', msg.content.toString());
channel1.ack(msg);
} else {
console.log('Consumer cancelled by server');
}
})
// Sender
const channel2 = await connection.createChannel()
setInterval(() => {
channel2.sendToQueue(AMQP_QUEUE_NAME, Buffer.from('something'))
}, 1000)
})
}
function launchServer() {
console.log('Launching Server...')
console.log('Connecting to MongoDB...')
const MONGODB_URI = process.env.MONGODB_URI?.toString() || ''
mongoose.connect(MONGODB_URI, {}, (err) => {
if (err) {
return console.error('Error connecting to Mongo DB !')
}
console.log('CONNECTED TO MONGO DB')
const port = parseInt(process.env.PORT?.toString() || '3000')
app.listen(port)
console.log('========= SERVER STARTED ========== PORT ' + port)
connectRabbitMQ()
})
}
launchServer()
Neither error nor success occur. The log doesn't get printed. Last log get printed is - Connecting to rabbit mq...
function connectRabbitMQ doesn't seems to be executed.
try appending this on last line of your code
connectRabbitMQ();
I am trying to pair a gateway to React native app using the react-native-ble-plx.
The below source code works fine in Android wheras in iOS, the bleManager.startDeviceScan() is not triggered. Nothing happens after this step.
Any help is much appreciated!
Source code:
const connectBLE = () => {
const subscription = bleManager.onStateChange(async (state) => {
if (state === 'PoweredOn') {
subscription.remove();
scanAndConnect();
}
};
}
const scanAndConnect = () => {
bleManager.startDeviceScan(null, null, async (error, device) => {
if (error) {
showToast(error, 'error');
console.log('Handle error - scanning will be stopped automatically');
return;
}
console.log('Devices');
console.log(device.name);
// Check if it is a device you are looking for based on device name
if (device.name === "BLE_0006") {
// Stop scanning as we have found the device.
bleManager.stopDeviceScan();
// Establish Device connection.
device
.connect()
.then((deviceData) => {
/** Show Toast on Device disconnect */
bleManager.onDeviceDisconnected(
deviceData.id,
(connectionError, connectionData) => {
if (connectionError) {
console.log(connectionError);
}
console.log('Device is disconnected');
console.log(connectionData);
},
);
/** Discover All Services and Characteristics */
return device.discoverAllServicesAndCharacteristics();
})
.then(async (deviceObject) => {
console.log('deviceObject');
console.log(deviceObject);
/** Subscribe for the Readable service */
device.monitorCharacteristicForService(
Enum.bleConnectionInfo.customServiceUUID,
Enum.bleConnectionInfo.readCharacteristicUUID,
(error, characteristic) => {
if (error) {
console.log('Error in monitorCharacteristicForService');
console.log(error.message);
return;
}
console.log(characteristic.uuid, characteristic.value);
]);
},
);
})
.catch((error) => {
console.warn(error);
showToast(error, 'error');
});
}
});
}
This might be helpful for someone!
The Issue was resolved by moving the bleManager() initialization outside the functional component.
I use Wix navigation v2, socket.io-client, react-native-background-time and react-native v59.10
first I register a HeadlessTask in index.js
if (Platform.OS === 'android') {
AppRegistry.registerHeadlessTask(
'backgroundTask',
() => backgroundTask
);
}
then in backgroundTask.js
import BackgroundTimer from 'react-native-background-timer';
import io from 'socket.io-client';
import { showLocalPushNotification } from 'helper/PushNotificationServices';
const URL = 'http://SOME_SECURE_URL'; //:)
const currentTimestamp = () => {
const d = new Date();
const z = n => (n.toString().length == 1 ? `0${n}` : n); // Zero pad
return `${d.getFullYear()}-${z(d.getMonth() + 1)}-${z(d.getDate())} ${z(
d.getHours()
)}:${z(d.getMinutes())}`;
};
let socket = io(URL, {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
reconnectionAttempts: Infinity,
autoConnect: true
});
socket.on('connect', () => {
showLocalPushNotification('Socket Say', `socket connect successfully`);
});
socket.on('disconnect', reason => {
console.log(reason);
socket.connect();
if (reason === 'io server disconnect') {
// the disconnection was initiated by the server, you need to reconnect manually
}
console.log('disconnect');
showLocalPushNotification('Socket Say', `socket disconnect :(`);
});
socket = socket.on('receive', async data => {
console.log('receive');
showLocalPushNotification(
'Socket Say',
`${currentTimestamp()}`
);
console.log(data);
});
socket.on('reconnect', attemptNumber => {
console.log('reconnect');
console.log(attemptNumber);
// ...
});
socket.on('reconnect_error', error => {
console.log('reconnect_error');
console.log(error);
// ...
});
socket.on('reconnect_failed', () => {
console.log('reconnect_failed');
// ...
});
BackgroundTimer.runBackgroundTimer(() => {
showLocalPushNotification(
'BackgroundTimer Say',
`${value || ''}\n${currentTimestamp()}`
);
socket.emit('send', { my: 'ok i get news' });
// this.socket.emit('send', { my: 'ok i get news' });
}, 1000 * 15 * 60);
in a real device, when the app goes to background, the socket will be disconnected after a while and I can't reconnect it again, how can I keep the connection alive?
I'm facing the same issue for long time, but I solved calling socket.connect() when I send app in background
AppState.addEventListener("change", this._handleAppStateChange.bind(this))
_handleAppStateChange(state) {
if (state == "background") {
socket.connect();
}
}
I would like connect 2 devices with WebRTC on localhost. All devices have no internet access. They are connected to a same local wifi.
I try this on React Native App.
In this context offline, do I need to trickle ICE candidates and addIceCandidate ? If I understund correctly, ICE candidates is for iceServer. But my case, iceServer is null (because i'm offline only, connected on same localhost wifi) :
const configuration = { iceServers: [{ urls: [] }] };
So actualty i exchange offer and answer, but after setRemoteDescription the answer, the connectionState stay on checking.
You can see my React Component :
constructor(props) {
super(props);
this.pc = new RTCPeerConnection(configuration);
}
state = initialState;
componentDidMount() {
const { pc } = this;
if (pc) {
this.setState({
peerCreated: true
});
}
this.setConnectionState();
pc.oniceconnectionstatechange = () => this.setConnectionState();
pc.onaddstream = ({ stream }) => {
if (stream) {
this.setState({
receiverVideoURL: stream.toURL()
});
}
};
pc.onnegotiationneeded = () => {
if (this.state.initiator) {
this.createOffer();
}
};
pc.onicecandidate = ({ candidate }) => {
if (candidate === null) {
const { offer } = this.state;
const field = !offer ? 'offer' : 'data';
setTimeout(() => {
alert('setTimeout started');
this.setState({
[field]: JSON.stringify(pc.localDescription)
});
}, 2000);
}
};
}
#autobind
setConnectionState() {
this.setState({
connectionState: this.pc.iceConnectionState
});
}
getUserMedia() {
MediaStreamTrack.getSources(() => {
getUserMedia(
{
audio: false,
video: true
},
this.getUserMediaSuccess,
this.getUserMediaError
);
});
}
#autobind
async getUserMediaSuccess(stream) {
const { pc } = this;
pc.addStream(stream);
await this.setState({ videoURL: stream.toURL() });
if (this.state.initiator) {
return this.createOffer();
}
return this.createAnswer();
}
getUserMediaError(error) {
console.log(error);
}
#autobind
logError(error) {
const errorArray = [...this.state.error, error];
return this.setState({
error: errorArray
});
}
/**
* Create offer
*
* #memberof HomeScreen
*/
#autobind
createOffer() {
const { pc } = this;
pc.createOffer()
.then(offer => pc.setLocalDescription(offer))
.then(() => {
this.setState({
offerCreated: true
});
})
.catch(this.logError);
}
/**
* Create anwser
*
* #memberof HomeScreen
*/
#autobind
async createAnswer() {
const { pc } = this;
const { data } = this.state;
if (data) {
const sd = new RTCSessionDescription(JSON.parse(data));
await this.setState({
offerImported: true
});
pc.setRemoteDescription(sd)
.then(() => pc.createAnswer())
.then(answer => pc.setLocalDescription(answer))
.then(() => {
this.setState({
answerCreated: true
});
})
.catch(this.logError);
}
}
#autobind
receiveAnswer() {
const { pc } = this;
const { data } = this.state;
const sd = new RTCSessionDescription(JSON.parse(data));
return pc
.setRemoteDescription(sd)
.then(() => {
this.setState({
answerImported: true
});
})
.catch(this.logError);
}
/**
* Start communication
*
* #param {boolean} [initiator=true]
* #returns
* #memberof HomeScreen
*/
#autobind
async start(initiator = true) {
if (!initiator) {
await this.setState({
initiator: false
});
}
return this.getUserMedia();
}
Anyone can help me?
No iceServers is fine on a LAN, but peers must still exchange at least one candidate: their host candidate (based on their machine's LAN IP address).
Either:
Trickle candidates using onicecandidate -> signaling -> addIceCandidate as usual, or
Out-wait the ICE process (a few seconds) before exchanging pc.localDescription.
It looks like you're attempting the latter. This approach works because...
Trickle ICE is an optimization.
The signaling (trickling) of individual ice candidates using onicecandidate, is an optimization meant to speed up negotiation. Once setLocalDescription succeeds, the browser's internal ICE Agent starts, inserting ICE candidates, as they're discovered, into the localDescription itself. Wait a few seconds to negotiate, and trickling isn't necessary at all: all ICE candidates will be in the offer and answer transmitted.
Your code
From your onicecandidate code it looks like you're already attempting to gather the localDescription after ICE completion (and you've written it to work from both ends):
pc.onicecandidate = ({ candidate }) => {
if (!candidate) {
const { offer } = this.state;
const field = !offer ? 'offer' : 'data';
this.setState({
[field]: JSON.stringify(pc.localDescription)
});
}
};
In the offerer side you've correctly commented out the equivalent code in createOffer:
pc.createOffer()
.then(offer => pc.setLocalDescription(offer))
.catch(this.logError);
// .then(() => {
// this.setState({
// offer: JSON.stringify(pc.localDescription)
// });
// });
But on the answerer side, you have not, and that's likely the problem:
createAnswer() {
const { pc } = this;
const { data } = this.state;
if (data) {
const sd = new RTCSessionDescription(JSON.parse(data));
pc.setRemoteDescription(sd)
.then(() => pc.createAnswer())
.then(answer => pc.setLocalDescription(answer))
.then(() => {
this.setState({
offer: JSON.stringify(pc.localDescription)
});
})
.catch(this.logError);
}
}
This means it sends an answer back immediately, before the answerer's ICE agent has had time to insert any candidates into the answer. This is probably why it fails.
On a side-note: Nothing appears to wait for getUserMedia to finish either, so answers likely won't contain any video either, depending on the timing of your getUserMediaSuccess function, which fails to add any tracks or streams to the connection. But assuming you're just doing data channels, this should work with my recommended fixes.