You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3KB

  1. /** Modified from original Node-Red source, for audio system visualization
  2. * vim: set ts=4:
  3. * Copyright 2014 IBM Corp.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. **/
  17. RED.comms = (function() {
  18. //var errornotification = null;
  19. //var subscriptions = {};
  20. //var ws;
  21. function connectWS() {
  22. /*
  23. var path = location.hostname+":"+location.port+document.location.pathname;
  24. path = path+(path.slice(-1) == "/"?"":"/")+"comms";
  25. path = "ws"+(document.location.protocol=="https:"?"s":"")+"://"+path;
  26. ws = new WebSocket(path);
  27. ws.onopen = function() {
  28. if (errornotification) {
  29. errornotification.close();
  30. errornotification = null;
  31. }
  32. for (var t in subscriptions) {
  33. if (subscriptions.hasOwnProperty(t)) {
  34. ws.send(JSON.stringify({subscribe:t}));
  35. }
  36. }
  37. }
  38. ws.onmessage = function(event) {
  39. var msg = JSON.parse(event.data);
  40. if (msg.topic) {
  41. for (var t in subscriptions) {
  42. if (subscriptions.hasOwnProperty(t)) {
  43. var re = new RegExp("^"+t.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$");
  44. if (re.test(msg.topic)) {
  45. var subscribers = subscriptions[t];
  46. if (subscribers) {
  47. for (var i=0;i<subscribers.length;i++) {
  48. subscribers[i](msg.topic,msg.data);
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. };
  56. ws.onclose = function() {
  57. if (errornotification == null) {
  58. //errornotification = RED.notify("<b>Error</b>: Lost connection to server","error",true);
  59. }
  60. setTimeout(connectWS,1000);
  61. }
  62. */
  63. }
  64. function subscribe(topic,callback) {
  65. //if (subscriptions[topic] == null) {
  66. //subscriptions[topic] = [];
  67. //}
  68. //subscriptions[topic].push(callback);
  69. //if (ws && ws.readyState == 1) {
  70. //ws.send(JSON.stringify({subscribe:topic}));
  71. //}
  72. }
  73. return {
  74. connect: connectWS,
  75. subscribe: subscribe
  76. }
  77. })();