1
0
Fork 0
mirror of https://api.glitch.com/git/yaswvc synced 2026-01-12 05:08:11 +00:00

👩🌊 Checkpoint

./server.js:3115263/17
./views/index.html:3115263/224
./public/main.js:3115263/1477
This commit is contained in:
Glitch (hello-express) 2020-05-25 18:33:24 +00:00
parent 67e55fbd02
commit 572986ece0
3 changed files with 42 additions and 2 deletions

View file

@ -1,16 +1,46 @@
// client-side js, loaded by index.html // client-side js, loaded by index.html
// run by the browser each time the page is loaded // run by the browser each time the page is loaded
let messagesEl = document.querySelector('.messages');
let peerIdEl = document.querySelector('#connect-to-peer');
let logMessage = (message) => {
let newMessage = document.createElement('div');
newMessage.innerText = message;
messagesEl.appendChild(newMessage);
};
let peer = new Peer({ let peer = new Peer({
host: '/', host: '/',
path: '/peerjs/myapp' path: '/peerjs/myapp'
}); });
peer.on('open', (id) => { peer.on('open', (id) => {
console.log('My peer ID is: ' + id); logMessage('My peer ID is: ' + id);
}); });
peer.on('error', (error) => { peer.on('error', (error) => {
console.error(error); console.error(error);
}); });
peer.on('connection', (conn) => {
logMessage('incoming peer connection!');
conn.on('data', (data) => {
logMessage(`received: ${data}`);
});
conn.on('open', () => {
conn.send('hello!');
});
});
console.log("hello world :o"); let connectToPeer = () => {
let peerId = peerIdEl.value;
logMessage(`Connecting to ${peerId}...`);
let conn = peer.connect(peerId);
conn.on('data', (data) => {
logMessage(`received: ${data}`);
});
conn.on('open', () => {
conn.send('hi!');
});
};
window.connectToPeer = connectToPeer;

View file

@ -21,6 +21,7 @@ const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port); console.log("Your app is listening on port " + listener.address().port);
}); });
// peerjs server
const peerServer = ExpressPeerServer(listener, { const peerServer = ExpressPeerServer(listener, {
debug: true, debug: true,
path: '/myapp' path: '/myapp'

View file

@ -14,6 +14,7 @@
<!-- import the webpage's stylesheet --> <!-- import the webpage's stylesheet -->
<link rel="stylesheet" href="/style.css"> <link rel="stylesheet" href="/style.css">
<!-- import libraries -->
<script src="/lib/peerjs.js"></script> <script src="/lib/peerjs.js"></script>
<!-- import the webpage's client-side javascript file --> <!-- import the webpage's client-side javascript file -->
@ -21,6 +22,14 @@
</head> </head>
<body> <body>
<input type="text" id="connect-to-peer" />
<button onclick="connectToPeer()">
Connect
</button>
<div class="messages">
</div>
<footer>Made with <a href="https://glitch.com">Glitch</a>!</footer> <footer>Made with <a href="https://glitch.com">Glitch</a>!</footer>