jellyfin-web/dashboard-ui/thirdparty/cordova/serverdiscovery.js

162 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-05-06 05:56:26 -07:00
(function (globalScope) {
2015-05-06 20:11:51 -07:00
function stringToArrayBuffer(string) {
// UTF-16LE
var buf = new ArrayBuffer(string.length * 2);
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = string.length; i < strLen; i++) {
bufView[i] = string.charCodeAt(i);
}
return buf;
}
function arrayBufferToString(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
2015-05-16 20:17:23 -07:00
function getResultCode(result) {
if (result != null && result.resultCode != null) {
return result.resultCode;
}
return result;
}
2015-05-12 21:55:19 -07:00
function findServersInternal(timeoutMs) {
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
var deferred = DeferredBuilder.Deferred();
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
var servers = [];
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
// Expected server properties
// Name, Id, Address, EndpointAddress (optional)
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
var chrome = globalScope.chrome;
2015-05-06 05:56:26 -07:00
2015-05-12 21:55:19 -07:00
if (!chrome) {
deferred.resolveWith(null, [servers]);
return deferred.promise();
}
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
var isTimedOut = false;
var timeout;
var socketId;
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
function startTimer() {
2015-05-06 20:11:51 -07:00
2015-05-16 20:17:23 -07:00
console.log('starting udp receive timer with timeout ms: ' + timeoutMs);
2015-05-12 21:55:19 -07:00
timeout = setTimeout(function () {
2015-05-06 20:11:51 -07:00
isTimedOut = true;
deferred.resolveWith(null, [servers]);
if (socketId) {
chrome.sockets.udp.onReceive.removeListener(onReceive);
chrome.sockets.udp.close(socketId);
}
}, timeoutMs);
2015-05-12 21:55:19 -07:00
}
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
function onReceive(info) {
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
console.log('ServerDiscovery message received');
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
console.log(info);
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
if (info.socketId == socketId) {
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
try {
2015-05-06 20:11:51 -07:00
var json = arrayBufferToString(info.data);
2015-05-12 21:55:19 -07:00
console.log('Server discovery json: ' + json);
2015-05-06 20:11:51 -07:00
var server = JSON.parse(json);
server.RemoteAddress = info.remoteAddress;
if (info.remotePort) {
server.RemoteAddress += ':' + info.remotePort;
}
servers.push(server);
2015-05-12 21:55:19 -07:00
} catch (err) {
console.log('Error receiving server info: ' + err);
2015-05-06 20:11:51 -07:00
}
}
2015-05-12 21:55:19 -07:00
}
var port = 7359;
console.log('chrome.sockets.udp.create');
chrome.sockets.udp.create(function (createInfo) {
socketId = createInfo.socketId;
console.log('chrome.sockets.udp.bind');
2015-05-16 20:17:23 -07:00
chrome.sockets.udp.bind(createInfo.socketId, '0.0.0.0', 0, function (result) {
2015-05-12 21:55:19 -07:00
2015-05-16 20:17:23 -07:00
if (getResultCode(result) != 0) {
2015-05-15 08:46:20 -07:00
console.log('bind fail: ' + result);
deferred.resolveWith(null, [servers]);
chrome.sockets.udp.close(createInfo.socketId);
return;
}
2015-05-12 21:55:19 -07:00
var data = stringToArrayBuffer('who is EmbyServer?');
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
console.log('chrome.sockets.udp.send');
chrome.sockets.udp.send(createInfo.socketId, data, '255.255.255.255', port, function (result) {
2015-05-06 20:11:51 -07:00
2015-05-16 20:17:23 -07:00
if (getResultCode(result) != 0) {
2015-05-12 21:55:19 -07:00
console.log('send fail: ' + result);
deferred.resolveWith(null, [servers]);
chrome.sockets.udp.close(createInfo.socketId);
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
} else {
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
console.log('sendTo: success ' + port);
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
startTimer();
chrome.sockets.udp.onReceive.addListener(onReceive);
}
});
});
});
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
return deferred.promise();
}
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
globalScope.ServerDiscovery = {
findServers: function (timeoutMs) {
var deferred = DeferredBuilder.Deferred();
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
deviceReadyPromise.done(function () {
2015-05-06 20:11:51 -07:00
2015-05-12 21:55:19 -07:00
findServersInternal(timeoutMs).done(function (result) {
deferred.resolveWith(null, [result]);
}).fail(function () {
deferred.reject();
2015-05-06 20:11:51 -07:00
});
});
2015-05-06 05:56:26 -07:00
return deferred.promise();
}
};
2015-05-12 21:55:19 -07:00
var deviceReadyDeferred = DeferredBuilder.Deferred();
var deviceReadyPromise = deviceReadyDeferred.promise();
document.addEventListener("deviceready", function () {
deviceReadyDeferred.resolve();
}, false);
2015-05-06 05:56:26 -07:00
})(window);