Strange connections: Unterschied zwischen den Versionen

Aus hyperdramatik
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 4: Zeile 4:


<source lang="java" line start="2" highlight="4-6">
<source lang="java" line start="2" highlight="4-6">
// using mqtt protokoll via shiftr.io
// using mqtt protokoll via shiftr.io
// https://docs.shiftr.io/manuals/processing/
// https://docs.shiftr.io/manuals/processing/
Zeile 25: Zeile 23:
//set fix color for players
//set fix color for players
int p1color, p2color;
int p1color, p2color;
// time variables for mqtt
// time variables for mqtt
long timeStamp;  
long timeStamp;  
int delayTime = 1000/35;
int delayTime = 1000/35;
//int mouseClick = 1;


void setup() {
void setup() {
Zeile 37: Zeile 32:
size(600,600);
size(600,600);
background(255);
background(255);
p1color = #AD2C2C;  // color for player 1
p1color = #AD2C2C;  // color for player 1
p2color = #230E9D; // color for player 2
p2color = #230E9D; // color for player 2
}
}
// RECEIVE messages of other player///
// RECEIVE messages of other player///
void clientConnected(){
void clientConnected(){
println("client connected");
println("client connected");
Zeile 63: Zeile 55:


//SEND my mouse x, y //
//SEND my mouse x, y //
void mouseDragged(){
void mouseDragged(){
   //the MQTT connection will close if you send more than 25 operations per second!
   //the MQTT connection will close if you send more than 25 operations per second!
Zeile 71: Zeile 62:
     client.publish(mePlayer,mouseX + "," + mouseY);
     client.publish(mePlayer,mouseX + "," + mouseY);
   }
   }
 
       strokeWeight(5); // set line thickness
       strokeWeight(5); // set line thickness
     
     // set right color for player //
     // set right color for player //
   if (mePlayer == "/playing-together/red/p1/xy"){
   if (mePlayer == "/playing-together/red/p1/xy"){
Zeile 81: Zeile 70:
   }  
   }  
   line (pmouseX,pmouseY,mouseX, mouseY);
   line (pmouseX,pmouseY,mouseX, mouseY);
 
     //set right color for player //
     //set right color for player //
     if (mePlayer == "/playing-together/red/p1/xy"){
     if (mePlayer == "/playing-together/red/p1/xy"){
Zeile 89: Zeile 77:
   }
   }
   line (otherMouseX, otherMouseY, mouseX, mouseY);
   line (otherMouseX, otherMouseY, mouseX, mouseY);
}
}


// DRAW  
// DRAW  
void draw() {
void draw() {
     strokeWeight(5);  //line thickness
     strokeWeight(5);  //line thickness
   
     // set right color for player //
     // set right color for player //
   if (mePlayer == "/playing-together/red/p1/xy"){
   if (mePlayer == "/playing-together/red/p1/xy"){
Zeile 114: Zeile 99:
   line (mouseX, mouseY, otherMouseX,otherMouseY);
   line (mouseX, mouseY, otherMouseX,otherMouseY);
   }
   }
// reset canvas //
// reset canvas //
void keyPressed(){
void keyPressed(){
   background(255);
   background(255);
   client.publish(mePlayer, 0 + "," + 0);
   client.publish(mePlayer, 0 + "," + 0);
}
}


//DEFECT REPORT ///
//DEFECT REPORT ///

Version vom 5. Mai 2020, 21:24 Uhr

Player1.png
Player2.png


<source lang="java" line start="2" highlight="4-6"> // using mqtt protokoll via shiftr.io // https://docs.shiftr.io/manuals/processing/

import mqtt.*; MQTTClient client;


/// Connection to other player String mePlayer = "/playing-together/red/p2/xy"; String otherPlayer = "/playing-together/red/p1/xy";

/// variables for position of others players mouse int otherMouseX = 0; int otherMouseY = 0; int otherPreviousMouseX = 0; //to keep track of last mouse position for drawing line int otherPreviousMouseY = 0;

//set fix color for players int p1color, p2color; // time variables for mqtt long timeStamp; int delayTime = 1000/35;

void setup() { client = new MQTTClient (this); client.connect ("mqtt://virtualEncounterSpace:avkVirtual@broker.shiftr.io","annavera"); size(600,600); background(255); p1color = #AD2C2C; // color for player 1 p2color = #230E9D; // color for player 2 } // RECEIVE messages of other player/// void clientConnected(){ println("client connected"); client.subscribe(otherPlayer); }

void messageReceived(String topic, byte [] payload){

   // the incoming MQTT "message" is a String: "xValue,yValue"
 // to unpack it we use the "split" function:
 String incomingPayload = new String(payload);
 String [] xy = split(incomingPayload, ',');
 if (int(xy[0])== 0 && int(xy[1])== 0){
     background(255);
 } else {
 println("new message: " + topic + " x: " + xy[0] + " y: " +xy[1]);
 otherMouseX = int(xy[0]);
 otherMouseY = int(xy[1]);

}}

//SEND my mouse x, y // void mouseDragged(){

 //the MQTT connection will close if you send more than 25 operations per second!
 //so we implement a delay to SEND my coordinates only every xxx milliseconds:
 if(millis() - timeStamp > delayTime){
   timeStamp = millis();
   client.publish(mePlayer,mouseX + "," + mouseY);
 }
     strokeWeight(5); // set line thickness
   // set right color for player //
 if (mePlayer == "/playing-together/red/p1/xy"){
 stroke(p1color); 
 } else if (mePlayer == "/playing-together/red/p2/xy") {
   stroke(p2color); 
 } 
 line (pmouseX,pmouseY,mouseX, mouseY);
    //set right color for player //
   if (mePlayer == "/playing-together/red/p1/xy"){
 stroke(p2color); 
 } else if (mePlayer == "/playing-together/red/p2/xy") {
   stroke(p1color); 
 }
 line (otherMouseX, otherMouseY, mouseX, mouseY);

}

// DRAW void draw() {

   strokeWeight(5);  //line thickness
   // set right color for player //
 if (mePlayer == "/playing-together/red/p1/xy"){
 stroke(p2color); 
 } else if (mePlayer == "/playing-together/red/p2/xy") {
   stroke(p1color); 
 }  
 line(otherPreviousMouseX, otherPreviousMouseY, otherMouseX, otherMouseY);
 otherPreviousMouseX = otherMouseX;  //update previous to be current
 otherPreviousMouseY = otherMouseY;  //update previous to be current
    if (mePlayer == "/playing-together/red/p1/xy"){
     stroke(p1color); 
     } else if (mePlayer == "/playing-together/red/p2/xy") {
     stroke(p2color); 
     } 
 line (mouseX, mouseY, otherMouseX,otherMouseY);
 }

// reset canvas // void keyPressed(){

 background(255);
 client.publish(mePlayer, 0 + "," + 0);

}

//DEFECT REPORT /// void connectionList(){

 println("connection lost");

}