-
-
Save jure/dd6aff4939a622983f57 to your computer and use it in GitHub Desktop.
100% CPU usage Chrome WebRTC DataChannel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="./overload-webrtc3.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription; | |
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; | |
var RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate; | |
var Client = function(config, signalingChannel) { | |
var self = this; | |
this.id = Math.random().toString(32); | |
this.remoteId = null; | |
this._pc = new RTCPeerConnection(config); | |
this._channel = null; | |
this._signalingChannel = signalingChannel; | |
this._requests = {}; | |
this._pc.onicecandidate = function(evt) { | |
if (evt.candidate) | |
self._signalingChannel.send(self.id, self.remoteId, JSON.stringify({ "candidate": evt.candidate })); | |
}; | |
this._pc.onnegotiationneeded = function() { | |
self._pc.createOffer(function(desc) { | |
self.localDescCreated(desc); | |
}, function(error) { | |
console.error("Failed to create offer:", error); | |
}); | |
}; | |
}; | |
Client.prototype = { | |
connect: function(remoteId) { | |
this.remoteId = remoteId; | |
this._channel = this._pc.createDataChannel(Math.random().toString(32)); | |
this.setupChannel(); | |
}, | |
localDescCreated: function(desc) { | |
var self = this; | |
this._pc.setLocalDescription(desc, function() { | |
self._signalingChannel.send(self.id, self.remoteId, JSON.stringify({ "sdp": self._pc.localDescription })); | |
}, function(error) { | |
console.error("Failed to set local description:", error); | |
}); | |
}, | |
onsignalingmessage: function(remoteId, json) { | |
var self = this; | |
if (!this.remoteId) { | |
this.remoteId = remoteId; | |
this._pc.ondatachannel = function(evt) { | |
self._channel = evt.channel; | |
self.setupChannel(); | |
}; | |
} | |
var message = JSON.parse(json); | |
if (message.sdp) { | |
this._pc.setRemoteDescription(new RTCSessionDescription(message.sdp), function() { | |
if (self._pc.remoteDescription.type == "offer") { | |
self._pc.createAnswer(function(desc) { | |
self.localDescCreated(desc); | |
}, function(error) { | |
console.error("Failed to create answer:", error); | |
}); | |
} | |
}, function(error) { | |
console.error("Failed to set remote description:", error); | |
}); | |
} else { | |
this._pc.addIceCandidate(new RTCIceCandidate(message.candidate), | |
function() {}, function(error) { | |
console.error("Failed to add ice candidate:", error); | |
}); | |
} | |
}, | |
setupChannel: function() { | |
var self = this; | |
var count = 0; | |
this._channel.onopen = function() { | |
self.sendRequest({type: 'ping'}); | |
}; | |
this._channel.onmessage = function(evt) { | |
self.sendRequest({type: 'pong'}); | |
count = count + 1; | |
if(count === 5) { | |
console.log('Disconnecting client', self.id); | |
self.destroy(); | |
} | |
}; | |
}, | |
sendRequest: function(request) { | |
var self = this; | |
this._channel.send(JSON.stringify(request)); | |
}, | |
destroy: function () { | |
var self = this; | |
self._pc.close(); | |
self._pc = null; | |
delete clients[self.id]; | |
} | |
}; | |
var clients = {}; | |
var signalingChannel = { | |
send: function(from, to, message) { | |
clients[to].onsignalingmessage(from, message); | |
} | |
}; | |
setInterval(function() { | |
var client1 = new Client({'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]}, | |
signalingChannel); | |
clients[client1.id] = client1; | |
var client2 = new Client({'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]}, | |
signalingChannel); | |
client1.connect(client2.id); | |
console.log('Connecting client 1 and 2.'); | |
clients[client2.id] = client2; | |
}, 200); | |
window.clients = clients; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment