HTML 5 - CORS, Web RTC (Part-IV)
In today's article, we will look at below HTML 5 features:
- CORS - Cross Origin Resource Sharing
- Web Real Time Interactivity (RTC)
1. CORS - Cross Origin Resource Sharing
- Allows restricted/blocks access to resource from another domain depending on the server configuration
- IE uses XDomainRequest object
- All other browsers: XMLHttpRequest object
- Example
- //setupfunction createCORSRequest(method, url) {var xhr = new XMLHttpRequest();if ("withCredentials" in xhr) {//only XMLHttpRequest2 object has withCredentials propertyxhr.open(method, url, true);} else if (typeof XDomainRequest != "undefined") {//Browser is IExhr = XDomainRequest();xhr.open(method, url);} else {xhr = null;}return xhr;}//callvar xhr = createCORSRequest("GET", apiUrl);if (!xhr) {throw new Error("xhr not supported");} else {//request is loadedxhr.onload = function () {var text = xhr.responseText;};xhr.onerror = function () {//Error};xhr.send();}
- Supports Browser to browser calling, P2P file sharing, video chat etc.
- Available for Chrome, Opera, Mozilla Firefox only
- APIs
- MediaStream:
- Synchronised streams of media
- Get access to user's camera & microphone.
- RTCPeerConnection: Video or Audio calling facility
- RTCDataChannel: Peer to peer communication
- Example:
- MediaStream
- function hasGetUserMedia() {return !!(navigator.getUserMedia ||navigator.webKitGetUserMedia ||navigator.mozGetUserMedia ||navigator.msGetUserMedia);}var video = document.querySelector("video");if (hasGetUserMedia()) {navigator.getUserMedia({ video: true, audio: true },function (localMediaStream) {video.src = window.URL.createObjectURL(localMediaStream);video.onloadedmetadata = function (e) {//doesnt fire in chrome when used with getUserMedia};},errorCallback);var errorCallback = function (e) {//error};} else {//not supportedvideo.src = falbackurl;}
Comments
Post a Comment