osc.js 4.0 KB

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