Guest User

Untitled

a guest
Jan 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. var dataServer;
  2. var pubKey = 'pub-c-a705a585-4407-4f88-8b83-ac846c45e13a';
  3. var subKey = 'sub-c-64587bc8-b0cf-11e6-a7bb-0619f8945a4f';
  4.  
  5. var mouseSend = 0.000;
  6.  
  7. //size of the active area
  8. var cSizeX = 900;
  9. var cSizeY = 600;
  10.  
  11. var rotZ = 0.0;
  12. var rotX = 0.0;
  13. var rotY = 0.0;
  14.  
  15.  
  16. //name used to sort your messages. used like a radio station. can be called anything
  17. var channelName = "phoneRotations";
  18.  
  19. function setup()
  20. {
  21. getAudioContext().resume();
  22. createCanvas(cSizeX, cSizeY);
  23. background(255);
  24.  
  25.  
  26.  
  27. // initialize pubnub
  28. dataServer = new PubNub(
  29. {
  30. publish_key : pubKey, //get these from the pubnub account online
  31. subscribe_key : subKey,
  32. ssl: true //enables a secure connection. This option has to be used if using the OCAD webspace
  33. });
  34.  
  35. //attach callbacks to the pubnub object to handle messages and connections
  36. dataServer.addListener({ message: readIncoming});
  37. dataServer.subscribe({channels: [channelName]});
  38. setInterval(sendMessage, 200);
  39.  
  40. }
  41.  
  42. function draw()
  43. {
  44. rotZ = rotationZ;
  45. rotX = rotationX;
  46. rotY = rotationY;
  47.  
  48. background(255);
  49. fill(0);
  50. text((rotZ+" "+rotX+" "+rotY), width/2, height/2);
  51.  
  52. }
  53.  
  54.  
  55. ///uses built in mouseClicked function to send the data to the pubnub server
  56. function sendMessage() {
  57.  
  58. mouseSend = mouseX+random(0,0.999);
  59. console.log(mouseSend);
  60. // Send Data to the server to draw it in all other canvases
  61. dataServer.publish(
  62. {
  63. channel: channelName,
  64. message:
  65. {
  66. rotZ: rotationZ,
  67. rotX: rotationX,
  68. rotY: rotationY //get the value from the text box and send it as part of the message
  69. }
  70. });
  71.  
  72. }
  73.  
  74. function readIncoming(inMessage) //when new data comes in it triggers this function,
  75. { // this works becsuse we subscribed to the channel in setup()
  76.  
  77. // simple error check to match the incoming to the channelName
  78. if(inMessage.channel == channelName)
  79. {
  80.  
  81. console.log(inMessage.message.messageText);
  82. }
  83. }
Add Comment
Please, Sign In to add comment