Coder Social home page Coder Social logo

p5.websocket's Introduction

A (moderately) simple p5.js library for connecting to websocket servers like the one at https://p5-websocket.glitch.me/.

Using p5.websocket

There's an example sketch at examples/sketch.js

First, download dist/p5.websocket.min.js and add it to your sketch's index.html file or use a CDN (content delivery network) to get the code straight from GitHub:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/abachman/p5.websocket/dist/p5.websocket.min.js"></script>
    <script src="sketch.js"></script>
  </head>
  <body></body>
</html>

A simple sketch that communicates with other sketches must include at least three things, a single call to connectWebsocket in the sketch setup, a user defined function messageReceived, and calls to sendMessage wherever you want your sketch to send data.

let myColor = [100, 100, 100];

function setup() {
  createCanvas(200, 200);
  noStroke();
  // connect to the chat.reasonable.systems p5 websocket server on the channel
  // named "p5.websocket-example". Every sketch on the same channel will get
  // messages from this sketch.
  connectWebsocket("wss://chat.reasonable.systems/p5.websocket-example");
}

function draw() {
  background(255);
  fill(myColor);
  ellipse(width / 2, height / 2, width);
}

function mousePressed() {
  // send a message on the connected channel
  sendMessage({ color: [random(255), random(255), random(255)] });
}

// receive a message on the connected channel
function messageReceived(data) {
  myColor = data.color;
}

You can play with a copy of this sketch here.

Library Functions

connectWebsocket(url, options)

sendMessage(message)

User Defined Functions

NOTE: uid in the functions below is the unique ID assigned to a given connection by p5-websocket-server.

onConnection(uid) when this sketch has finished connecting. The uid received is this sketch's own.

onDisconnection when this sketch has finished disconnecting. Usually only if the websocket server restarts or internet goes in-and-out.

connectReceived(uid) when another session connects.

disconnectReceived(uid) when another session disconnects.

messageReceived(data, uid) when a message is received from a session. If config includes { echo: true } (the default) then the given sketch will receive its own messages.

Contributing

Clone this repository and run yarn install.

Then run yarn dev.

Serve example-dist/ as static.

Other Stuff

Check out the p5.websocket reference server.

p5.websocket's People

Contributors

abachman avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.