/p2p-test

Primary LanguageJavaScript

PeerJS WebRTC Game

This is a simple example of how to set up a WebRTC game using the PeerJS library.

Setup

To run this code, you'll need to have Node.js installed on your machine.

Clone this repository to your local machine. Navigate to the repository directory in your terminal. Run npm install to install the required dependencies. Run npm start to start the server. Usage Once the server is running, you can open the game in your browser by navigating to http://localhost:3000.

The game allows you to connect to another peer using their PeerJS ID. To connect to another peer:

Enter their PeerJS ID in the "Peer ID" input field. Click the "Connect" button. Once you've connected to another peer, you can send data back and forth between peers by entering text in the "Send Data" input field and clicking the "Send" button.

You can also view incoming data in the "Received Data" box.

Code Explanation

The code sets up a simple PeerJS instance, connects to the signaling server, and listens for incoming connections.

When a connection is established, the code sets up a dataConnection object, listens for incoming data on the connection, and sends a response when data is received.

When the user clicks the "Connect" button, the code connects to another peer using their PeerJS ID. Once the connection is established, the code sends and receives data between peers.

Conclusion

This is a very basic example of how to set up a WebRTC game using the PeerJS library. Depending on the complexity of your game, you may need to add additional logic to handle things like game state synchronization, latency compensation, and more.

However, this example should give you a good starting point for setting up a simple WebRTC game using PeerJS.