osc.js 4.1 KB

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