Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import processing.core.*;
  2. import processing.data.*;
  3. import processing.event.*;
  4. import processing.opengl.*;
  5.  
  6. import org.java_websocket.client.WebSocketClient;
  7. import org.java_websocket.handshake.ServerHandshake;
  8. import java.net.URI;
  9.  
  10. import java.util.HashMap;
  11. import java.util.ArrayList;
  12. import java.io.File;
  13. import java.io.BufferedReader;
  14. import java.io.PrintWriter;
  15. import java.io.InputStream;
  16. import java.io.OutputStream;
  17. import java.io.IOException;
  18.  
  19. public class WebPaint extends PApplet {
  20.  
  21.  
  22.  
  23.  
  24.  
  25. Client client;
  26. boolean isOpen = false;
  27. int x = -50;
  28. int y = -50;
  29.  
  30. public void setup() {
  31.  
  32. background(100);
  33. fill(0);
  34. try {
  35. client = new Client(new URI("wss://paint.unubo.cloud/socket.io/?EIO=3&transport=websocket"));
  36. client.connect();
  37. }
  38. catch(Exception e) {
  39. println(e);
  40. }
  41. }
  42.  
  43. public void draw() {
  44. ellipse(x, y, 10, 10);
  45. }
  46.  
  47. public void mouseDragged() {
  48. if (isOpen) {
  49. client.send("42["message",{"x":" + mouseX + ","y":" + mouseY + "}]");
  50. }
  51. }
  52.  
  53. public void keyPressed() {
  54. client.connect();
  55. }
  56. class Client extends WebSocketClient {
  57. public Client(URI serverURI) {
  58. super(serverURI);
  59. }
  60. public void onError(Exception e) {
  61. println(e);
  62. }
  63. public void onClose(int reason, String details, boolean remote) {
  64. println("Closed: " + reason + " " + details);
  65. isOpen = false;
  66. //exit();
  67. }
  68. public void onOpen(ServerHandshake hs) {
  69. println("Connected");
  70. isOpen = true;
  71. }
  72. public void onMessage(String message) {
  73. if (message.length() > 2 && message.substring(0, 2).equals("42")) {
  74. String data = message.substring(13, message.length() - 1);
  75. JSONObject json = JSONObject.parse(data);
  76. println(json.toString());
  77. x = json.getInt("x");
  78. y = json.getInt("y");
  79. ellipse(json.getInt("x"), json.getInt("y"), 10, 10);
  80. }
  81. }
  82. }
  83. public void settings() { size(600, 600); }
  84. static public void main(String[] passedArgs) {
  85. String[] appletArgs = new String[] { "WebPaint" };
  86. if (passedArgs != null) {
  87. PApplet.main(concat(appletArgs, passedArgs));
  88. } else {
  89. PApplet.main(appletArgs);
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement