GithubHelp home page GithubHelp logo

metad4x2 / multiplayer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from exokitxr/multiplayer

0.0 0.0 0.0 1.16 MB

P2P multiplayer Exokit with XRChannelConnection + XRPeerConnection

Home Page: https://multiplayer.exokit.org

JavaScript 98.39% HTML 1.59% Shell 0.02%

multiplayer's Introduction

Exokit Multiplayer

WebRTC multiplayer presence.

How it works

import {XRChannelConnection, XRPeerConnection} from 'https://multiplayer.exokit.org/multiplayer.js';

const presenceUrl = `wss://presence.exokit.org/?u=username&c=channel`;
const xrChannelConnection = new XRChannelConnection(presenceUrl, {
  // all options are optional

  // navigator.mediaDevices.getUserMedia({audio: true}); // microphone input for voice chat
  microphoneMediaStream,
});
xrChannelConnection.addEventListener('open', () => {
  console.log('xr channel open');
});
xrChannelConnection.addEventListener('error', err => {
  console.warn('xr channel error', err);
});
xrChannelConnection.addEventListener('peerconnection', e => {
  const peerConnection = e.detail; // XRPeerConnection

  let updateInterval = 0;
  peerConnection.addEventListener('open', () => {
    console.log('peer connection open', peerConnection);

    // send updates
    updateInterval = setInterval(() => {
      const hmd = {
        position: [0, 0, 0],
        quaternion: [0, 0, 0,1 ],
      };
      const gamepads = [
        {
          position: localVector.copy(rig.inputs.leftGamepad.position).divideScalar(heightFactor).toArray(),
          quaternion: rig.inputs.leftGamepad.quaternion.toArray(),
          pointer: rig.inputs.leftGamepad.pointer,
          grip: rig.inputs.leftGamepad.grip,
          visible: true,
        },
        {
          position: localVector.copy(rig.inputs.rightGamepad.position).divideScalar(heightFactor).toArray(),
          quaternion: rig.inputs.rightGamepad.quaternion.toArray(),
          pointer: rig.inputs.rightGamepad.pointer,
          grip: rig.inputs.rightGamepad.grip,
          visible: true,
        },
      ];
    }, 100);

    // send custom message
    peerConnection.send(JSON.stringify({
      method: 'customData',
      payload: 'lol',
    }));
  });
  peerConnection.addEventListener('close', () => {
    console.log('peer connection close', peerConnection);

    clearInterval(updateInterval);
  });
  peerConnection.addEventListener('pose', e => {
    const {detail: data} = e;
    const {hmd, gamepads} = data;

    // receive update in the same format as sent
  });
  peerConnection.addEventListener('message', e => {
    const data = JSON.parse(e.data);
    const {method} = data;
    if (method === 'customData') {
      const {payload} = data;

      // receive message in the same format as sent
    } else {
      console.warn(`unknown method: ${method}`);
    }
  });
});

xrChannelConnection.setMicrophoneMediaStream(microphoneMediaStream); // set microphoneMediaStream separately

multiplayer's People

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.