jellyfin-web/dashboard-ui/bower_components/emby-apiclient/serverdiscovery-winjs.js
Luke Pulverenti 8a6884abef minify
2017-01-27 18:07:14 -05:00

1 line
1.6 KiB
JavaScript

define([],function(){"use strict";function listenerSession(resolve,timeoutMs){function closeListenerSocket(){timeout&&clearTimeout(timeout),null!==listenerSocket&&(listenerSocket.close(),listenerSocket=null),resolve(servers)}function startListener(eventObject){listenerSocket=new Windows.Networking.Sockets.DatagramSocket,listenerSocket.addEventListener("messagereceived",onMessageReceived),listenerSocket.bindServiceNameAsync("").done(function(){sendMessage(stringToSend)},onError)}function sendMessage(txt){try{var remoteHostname=new Windows.Networking.HostName("255.255.255.255");listenerSocket.getOutputStreamAsync(remoteHostname,serviceName).done(function(outputStream){try{var writer=new Windows.Storage.Streams.DataWriter(outputStream);writer.writeString(txt),writer.storeAsync().done(function(){timeout=setTimeout(closeListenerSocket,timeoutMs)},onError)}catch(exception){onError("Error sending message: "+exception.message)}},onError)}catch(exception){onError("Error sending message outer: "+exception.message)}}function onMessageReceived(eventArguments){try{var stringLength=eventArguments.getDataReader().unconsumedBufferLength,receivedMessage=eventArguments.getDataReader().readString(stringLength);if(receivedMessage===stringToSend)return;var server=JSON.parse(receivedMessage);servers.push(server)}catch(exception){onError("Error receiving message: "+exception)}}function onError(reason){closeListenerSocket()}var timeout,listenerSocket=null,serviceName="7359",servers=[],stringToSend="who is EmbyServer?|emby";startListener()}return{findServers:function(timeoutMs){return new Promise(function(resolve,reject){new listenerSession(resolve,timeoutMs)})}}});