osc.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. firedEvent: function (nodeID, eventName, eventParameters) {
  19. if (eventName == 'sendOSC'){
  20. var clientThatSatProperty = self.kernel.client();
  21. var me = self.kernel.moniker();
  22. // If the transform property was initially updated by this view....
  23. if (clientThatSatProperty == me) {
  24. if (self.osc !== null) {
  25. if (this.getStatus() == 1) {
  26. self.port.send(eventParameters[0]);
  27. console.log('send: ' + eventParameters[0]);
  28. }
  29. }
  30. }
  31. }
  32. },
  33. /*
  34. * Receives incoming messages
  35. */
  36. calledMethod: function( nodeID, methodName, methodParameters, methodValue ) {
  37. // if (methodName == "sendOSC") {
  38. // if (self.osc !== null) {
  39. // if (this.getStatus() == 1) {
  40. // self.port.send(methodParameters[0]);
  41. // console.log('send: ' + methodParameters);
  42. // }
  43. // }
  44. // }
  45. if (methodName == "sendOSCBundle") {
  46. if (self.osc !== null) {
  47. if (this.getStatus() == 1) {
  48. self.port.send({
  49. timeTag: self.osc.timeTag(1), // Schedules this bundle 60 seconds from now.
  50. packets: [methodParameters[0]]
  51. }
  52. );
  53. console.log('send: ' + methodParameters[0]);
  54. }
  55. }
  56. //console.log("send OSC!!!");
  57. }
  58. },
  59. setOSCHostAndPort: function(h,p) {
  60. this.hostValue = h;
  61. this.portValue = p;
  62. },
  63. connect: function() {
  64. this.disconnect();
  65. var url = 'wss://' + this.hostValue + ':' + this.portValue;
  66. if (this.hostValue == 'localhost'){
  67. url = 'ws://' + this.hostValue + ':' + this.portValue
  68. }
  69. this.port = new osc.WebSocketPort({
  70. url: url //'wss://' + this.hostValue + ':' + this.portValue
  71. //url: "ws://localhost:8081"
  72. });
  73. this.port.on("message", function (oscMessage) {
  74. console.log("message", oscMessage);
  75. findAllNodesWithOSC(oscMessage);
  76. });
  77. this.port.open();
  78. },
  79. disconnect: function() {
  80. console.log('disconnect');
  81. if (this.port !== null)
  82. this.port.close(1000, 'manual close');
  83. },
  84. getStatus: function() {
  85. if (self.port){
  86. return self.port.socket.readyState
  87. }
  88. return 3
  89. }
  90. })
  91. function findAllNodesWithOSC(oscMessage){
  92. let appID = vwf.application();
  93. var oscSceneProp = vwf.getProperty(appID, 'osc');
  94. if (oscSceneProp !== undefined){
  95. if (oscSceneProp){
  96. //console.log('now callMethod');
  97. vwf_view.kernel.callMethod(appID, 'getOSC', [oscMessage]);
  98. }
  99. }
  100. var children = vwf.children(appID);
  101. children.forEach(function(child){
  102. var oscprop = vwf.getProperty(child, 'osc');
  103. if (oscprop !== undefined){
  104. if (oscprop){
  105. //console.log('now callMethod');
  106. vwf_view.kernel.callMethod(child, 'getOSC', [oscMessage]);
  107. }
  108. }
  109. })
  110. // var scene = _Editor.getNode(Engine.application());
  111. // for (var i in scene.children) {
  112. // if (scene.children[i].properties.DisplayName.indexOf("leapmotion") > -1) {
  113. // this.leapHandsID = (scene.children[i].id);
  114. // }
  115. // }
  116. }
  117. });