osc.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
  5. */
  6. // VWF & OSC driver
  7. define(["module", "vwf/view", "vwf/view/oscjs/dist/osc-module"], function(module, view, osc) {
  8. return view.load(module, {
  9. initialize: function() {
  10. self = this;
  11. this.osc = osc;
  12. this.portValue = '8081';
  13. this.hostValue = 'localhost';
  14. this.port = null;
  15. //window._osc = this.osc;
  16. window._OSCManager = this;
  17. },
  18. /*
  19. * Receives incoming messages
  20. */
  21. // calledMethod: function(id, name, params) {
  22. // },
  23. setOSCHostAndPort: function(h,p) {
  24. this.hostValue = h;
  25. this.portValue = p;
  26. },
  27. connect: function() {
  28. this.disconnect();
  29. this.port = new osc.WebSocketPort({
  30. url: 'ws://' + this.hostValue + ':' + this.portValue
  31. //url: "ws://localhost:8081"
  32. });
  33. this.port.on("message", function (oscMessage) {
  34. console.log("message", oscMessage);
  35. findAllNodesWithOSC(oscMessage);
  36. });
  37. this.port.open();
  38. },
  39. disconnect: function() {
  40. console.log('disconnect');
  41. if (this.port !== null)
  42. this.port.close(1000, 'manual close');
  43. },
  44. getStatus: function() {
  45. if (this.port){
  46. return this.port.socket.readyState
  47. }
  48. return 3
  49. }
  50. })
  51. function findAllNodesWithOSC(oscMessage){
  52. let appID = vwf.application();
  53. var oscSceneProp = vwf.getProperty(appID, 'osc');
  54. if (oscSceneProp !== undefined){
  55. if (oscSceneProp){
  56. //console.log('now callMethod');
  57. vwf_view.kernel.callMethod(appID, 'getOSC', [oscMessage]);
  58. }
  59. }
  60. var children = vwf.children(appID);
  61. children.forEach(function(child){
  62. var oscprop = vwf.getProperty(child, 'osc');
  63. if (oscprop !== undefined){
  64. if (oscprop){
  65. //console.log('now callMethod');
  66. vwf_view.kernel.callMethod(child, 'getOSC', [oscMessage]);
  67. }
  68. }
  69. })
  70. // var scene = _Editor.getNode(Engine.application());
  71. // for (var i in scene.children) {
  72. // if (scene.children[i].properties.DisplayName.indexOf("leapmotion") > -1) {
  73. // this.leapHandsID = (scene.children[i].id);
  74. // }
  75. // }
  76. }
  77. });