index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var app = {
  20. // Application Constructor
  21. initialize: function() {
  22. document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
  23. },
  24. // deviceready Event Handler
  25. //
  26. // Bind any cordova events here. Common events are:
  27. // 'pause', 'resume', etc.
  28. onDeviceReady: function() {
  29. this.receivedEvent('deviceready');
  30. var diont = Diont();
  31. // ======
  32. // Listen for announcements and renouncements in services
  33. // ======
  34. diont.on("serviceAnnounced", function(serviceInfo) {
  35. // A service was announced
  36. // This function triggers for services not yet available in diont.getServiceInfos()
  37. // serviceInfo is an Object { isOurService : Boolean, service: Object }
  38. // service.name, service.host and service.port are always filled
  39. console.log("A new service was announced", serviceInfo.service);
  40. // List currently known services
  41. console.log("All known services", diont.getServiceInfos());
  42. alert('found'+ serviceInfo.service.host+" "+serviceInfo.service.port)
  43. });
  44. diont.on("serviceRenounced", function(serviceInfo) {
  45. console.log("A service was renounced", serviceInfo.service);
  46. console.log("All known services", diont.getServiceInfos());
  47. });
  48. },
  49. // Update DOM on a Received Event
  50. receivedEvent: function(id) {
  51. var parentElement = document.getElementById(id);
  52. var listeningElement = parentElement.querySelector('.listening');
  53. var receivedElement = parentElement.querySelector('.received');
  54. listeningElement.setAttribute('style', 'display:none;');
  55. receivedElement.setAttribute('style', 'display:block;');
  56. console.log('Received Event: ' + id);
  57. }
  58. };
  59. app.initialize();