Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.core.*;
- import processing.data.*;
- import processing.event.*;
- import processing.opengl.*;
- import org.java_websocket.client.WebSocketClient;
- import org.java_websocket.handshake.ServerHandshake;
- import java.net.URI;
- import java.util.HashMap;
- import java.util.ArrayList;
- import java.io.File;
- import java.io.BufferedReader;
- import java.io.PrintWriter;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.IOException;
- public class WebPaint extends PApplet {
- Client client;
- boolean isOpen = false;
- int x = -50;
- int y = -50;
- public void setup() {
- background(100);
- fill(0);
- try {
- client = new Client(new URI("wss://paint.unubo.cloud/socket.io/?EIO=3&transport=websocket"));
- client.connect();
- }
- catch(Exception e) {
- println(e);
- }
- }
- public void draw() {
- ellipse(x, y, 10, 10);
- }
- public void mouseDragged() {
- if (isOpen) {
- client.send("42["message",{"x":" + mouseX + ","y":" + mouseY + "}]");
- }
- }
- public void keyPressed() {
- client.connect();
- }
- class Client extends WebSocketClient {
- public Client(URI serverURI) {
- super(serverURI);
- }
- public void onError(Exception e) {
- println(e);
- }
- public void onClose(int reason, String details, boolean remote) {
- println("Closed: " + reason + " " + details);
- isOpen = false;
- //exit();
- }
- public void onOpen(ServerHandshake hs) {
- println("Connected");
- isOpen = true;
- }
- public void onMessage(String message) {
- if (message.length() > 2 && message.substring(0, 2).equals("42")) {
- String data = message.substring(13, message.length() - 1);
- JSONObject json = JSONObject.parse(data);
- println(json.toString());
- x = json.getInt("x");
- y = json.getInt("y");
- ellipse(json.getInt("x"), json.getInt("y"), 10, 10);
- }
- }
- }
- public void settings() { size(600, 600); }
- static public void main(String[] passedArgs) {
- String[] appletArgs = new String[] { "WebPaint" };
- if (passedArgs != null) {
- PApplet.main(concat(appletArgs, passedArgs));
- } else {
- PApplet.main(appletArgs);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement