Created
July 25, 2014 04:53
-
-
Save mlakkadshaw/a9eed25e610f1dbb5bcd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
collaborativeLearningApp.service('screenShare', function($q, mainService) { | |
var $scope = undefined; | |
var that = this; | |
this.socket = mainService.socket; | |
this.getScope = function(s, room) { | |
$scope = s; | |
that.loadCount = 0; | |
var countx = 0, | |
county = 0; | |
console.log("Link called"); | |
function drawImage(image) { | |
var img = new Image; | |
var elm = $('#screenShare canvas'); | |
console.log(elm); | |
var ctx = elm[0].getContext('2d'); | |
img.onload = function() { | |
that.loadCount = that.loadCount + 1; | |
if (that.loadCount === 0) { | |
elm[0].width = img.width; | |
elm[0].height = img.height; | |
} | |
ctx.drawImage(img, 0, 0); | |
}; | |
img.src = image; | |
} | |
<<<<<<< HEAD | |
var SERVER_URL = "ws://warm-chamber-3147.herokuapp.com/"; //"ws://" + window.location.hostname + ":8080/"; | |
======= | |
var SERVER_URL = "ws://" + tm.screenshareServer; | |
console.log(tm.screenshareServer); | |
>>>>>>> 2901f21c3fcf4357fb16211d2ba50265a43fc41e | |
// var SERVER_URL = "ws://127.0.0.1:8080/"; | |
var connection = {}; | |
that.socket.on('screenShareCode', function(data) { | |
if (!that.isConnector) { | |
$('.lesson-control a').each(function() { | |
if ($(this).text() == 'Screen Share') { | |
$(this).addClass('active'); | |
$scope.$apply(function() { | |
$scope.template = 'ScreenShare' | |
}); | |
} | |
}); | |
handleConnection(SERVER_URL + data.code, false); | |
} | |
}); | |
$scope.shareScreen = function() { | |
$('.lesson-control a').each(function() { | |
if ($(this).text() == 'Screen Share') { | |
$(this).addClass('active'); | |
$scope.template = 'ScreenShare' | |
} | |
}); | |
handleConnection(SERVER_URL + $scope.code, true); | |
}; | |
function handleConnection(url, isShareMode) { | |
var deferred = $q.defer(); | |
var connection = new WebSocket(url); | |
connection.binaryType = 'arraybuffer'; | |
connection.onopen = function() { | |
deferred.resolve(connection); | |
connection.send('{ "auth": { "apikey": "' + $scope.apikey + '", "appid": "' + $scope.appid + '" }}'); | |
}; | |
connection.onmessage = function(e) { | |
console.log(typeof e.data); | |
if (typeof e.data === 'string') { | |
if (isShareMode) { | |
that.isConnector = true; | |
if (e.data !== "") { | |
function docWriteWrapper(func) { | |
var writeTo = document.createElement('del'), | |
oldwrite = document.write, | |
content = ''; | |
writeTo.id = "me"; | |
document.write = function(text) { | |
content += text; | |
}; | |
func(); | |
writeTo.innerHTML += content; | |
document.write = oldwrite; | |
document.body.appendChild(writeTo); | |
} | |
var attributes = { //codebase:'/embed/script/MainClass.class', | |
code: 'MainClass.class', | |
archive: tm.staticUrl + 'client/lib/ScreenSharingApple.jar', | |
width: 1, | |
height: 1 | |
}; | |
that.socket.emit('screenShareCode', { | |
code: e.data, | |
room: room | |
}); | |
//$scope.screen_code = "Your code is: " + e.data;// code | |
console.log(e.data); | |
var parameters = { | |
Message: e.data, | |
serverURL: 'warm-chamber-3147.herokuapp.com', //window.location.hostname | |
port: 80 | |
}; | |
var version = '1.6'; | |
docWriteWrapper(function() { | |
if (document.applets.length === 0) | |
window.deployJava.writeAppletTag(attributes, parameters, version); | |
}); | |
} | |
} | |
} | |
if (!that.isConnector) { | |
var image = arrayBufferToDataUri(e.data); | |
console.log("Data Recieved"); | |
drawImage(image); | |
} | |
}; | |
return deferred.promise; | |
} | |
function arrayBufferToDataUri(arrayBuffer) { | |
var base64 = '', | |
encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', | |
bytes = new Uint8Array(arrayBuffer), | |
byteLength = bytes.byteLength, | |
byteRemainder = byteLength % 3, | |
mainLength = byteLength - byteRemainder, | |
a, b, c, d, chunk; | |
for (var i = 0; i < mainLength; i = i + 3) { | |
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; | |
a = (chunk & 16515072) >> 18; | |
b = (chunk & 258048) >> 12; | |
c = (chunk & 4032) >> 6; | |
d = chunk & 63; | |
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]; | |
} | |
if (byteRemainder == 1) { | |
chunk = bytes[mainLength]; | |
a = (chunk & 252) >> 2; | |
b = (chunk & 3) << 4; | |
base64 += encodings[a] + encodings[b] + '=='; | |
} else if (byteRemainder == 2) { | |
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]; | |
a = (chunk & 16128) >> 8; | |
b = (chunk & 1008) >> 4; | |
c = (chunk & 15) << 2; | |
base64 += encodings[a] + encodings[b] + encodings[c] + '='; | |
} | |
return "data:image/png;base64," + base64; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment