P5JS Socket.IO

test 


The video above will be about node servers and sending messages to screens of clients with JS.

Step 1: Add code to client to send a message, your code should look like this:

1  var socket;
2
3  function setup() {
4      createCanvas(width, height);
5      background(color);
6
7      socket = io.connect(link)
8  }
9
10  function draw() {
11      noStroke();
12      fill(color);
13      ellipse(mouseX, mouseY, width, height);
14  }

Step 2: Go to line 9 then add a new line of code called : function mouseDragged() {} and then add the following code inside the function: console.log('Sending: ' + mouseX + "," + mouseY);

When you move your mouse, you should a set of coordinates getting printed in the console,
(example: Sending: 305,273)
You will also have to add the entire function draw() into function MouseDragged()

Step 3: Go inside your function MouseDragged and then add this line of code
            var data = {
                x: mouseX,
                y: mouseY
           }

           socket.emit('mouse', data);

If you want to learn more please watch the video above.

ความคิดเห็น