Fix ICE candidate handling for Node.js compatibility

Handle both browser and Node.js (wrtc) environments when processing ICE candidates. Browsers provide toJSON() method on candidates, but wrtc library candidates are already plain objects. Check for toJSON() existence before calling it.

Fixes: TypeError: event.candidate.toJSON is not a function in Node.js
This commit is contained in:
2025-12-13 15:19:45 +01:00
parent 0aa9921941
commit eb280e3826

View File

@@ -365,10 +365,17 @@ export class Rondevu {
pc.onicecandidate = async (event) => { pc.onicecandidate = async (event) => {
if (event.candidate) { if (event.candidate) {
try { try {
// Handle both browser and Node.js (wrtc) environments
// Browser: candidate.toJSON() exists
// Node.js wrtc: candidate is already a plain object
const candidateData = typeof event.candidate.toJSON === 'function'
? event.candidate.toJSON()
: event.candidate
await this.api.addOfferIceCandidates( await this.api.addOfferIceCandidates(
serviceFqn, serviceFqn,
offerId, offerId,
[event.candidate.toJSON()] [candidateData]
) )
} catch (err) { } catch (err) {
console.error('[Rondevu] Failed to send ICE candidate:', err) console.error('[Rondevu] Failed to send ICE candidate:', err)