Here's an HTML file that implements a simple peer-to-peer chat application using WebRTC. Users can type a word to connect, and then send messages to each other directly:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>P2P Chat</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
#connectSection, #chatSection { margin-bottom: 20px; }
#chatSection { display: none; }
input, button { margin: 5px 0; }
#messages { border: 1px solid #ccc; height: 300px; overflow-y: auto; padding: 10px; }
</style>
</head>
<body>
<div id="connectSection">
<input id="connectWord" placeholder="Enter a word to connect" type="text"/>
<button onclick="connect()">Connect</button>
</div>
<div id="chatSection">
<div id="messages"></div>
<input id="messageInput" placeholder="Type a message" type="text"/>
<button onclick="sendMessage()">Send</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/peerjs/1.3.2/peerjs.min.js"></script>
<script>
let peer;
let conn;
function connect() {
const word = document.getElementById('connectWord').value;
if (!word) return alert('Please enter a word');
peer = new Peer(word);
peer.on('open', (id) => {
console.log('My peer ID is: ' + id);
tryToConnect(word);
});
peer.on('connection', (connection) => {
conn = connection;
setupConnection();
});
}
function tryToConnect(word) {
conn = peer.connect(word);
if (conn) setupConnection();
}
function setupConnection() {
conn.on('open', () => {
document.getElementById('connectSection').style.display = 'none';
document.getElementById('chatSection').style.display = 'block';
});
conn.on('data', (data) => {
addMessage('Peer: ' + data);
});
}
function sendMessage() {
const message = document.getElementById('messageInput').value;
if (!message) return;
conn.send(message);
addMessage('You: ' + message);
document.getElementById('messageInput').value = '';
}
function addMessage(message) {
const messagesDiv = document.getElementById('messages');
messagesDiv.innerHTML += '<p>' + message + '</p>';
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
</script>
</body>
</html>
This HTML file creates a simple peer-to-peer chat application using WebRTC and the PeerJS library. Here's how it works:
Users enter a word in the input field and click "Connect".
The application creates a peer connection using the entered word as the peer ID.
It then tries to connect to another peer with the same word.
Once connected, the chat interface appears, allowing users to send messages.
To use this:
Save this HTML file locally.
Open the file in two different browsers or devices.
Enter the same word on both devices and click "Connect".
Once connected, you can start sending messages between the two peers.
Note that this is a basic implementation and may not work in all network environments due to NAT traversal issues. For a more robust solution, you might need to use STUN/TURN servers[7].
Also, keep in mind that this implementation doesn't include any encryption or security measures. For a production application, you should implement proper security protocols[13].