🤖🍲 Checkpoint

./room.js:530995/86
./server.js:530995/1413
./package.json:530995/1484
./views/room.handlebars:530995/1594
This commit is contained in:
Glitch (hello-express) 2020-04-09 11:04:28 +00:00
parent b337088cb6
commit 1abee4f9d7
5 changed files with 47 additions and 5 deletions

View File

@ -13,7 +13,8 @@
"express": "^4.17.1", "express": "^4.17.1",
"express-handlebars": "^4.0.3", "express-handlebars": "^4.0.3",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"express-fingerprint": "^1.1.3" "express-fingerprint": "^1.1.3",
"ws": "^7.2.3"
}, },
"engines": { "engines": {
"node": "12.x" "node": "12.x"

View File

@ -26,3 +26,4 @@ function addParticipant(roomId, participantId, participantName) {
rooms[roomId] = room; rooms[roomId] = room;
} }
module.exports = {getOrCreateRoom, addParticipant}

View File

@ -1,11 +1,14 @@
const express = require("express"); const express = require("express");
const exphbs = require('express-handlebars'); const exphbs = require('express-handlebars');
const bodyParser = require('body-parser') const bodyParser = require('body-parser');
const Fingerprint = require('express-fingerprint') const Fingerprint = require('express-fingerprint');
const WebSocket = require('ws');
const http = require('http');
const rooms = require('./room'); const rooms = require('./room');
const app = express(); const app = express();
const server = http.createServer(app);
app.use(Fingerprint({ app.use(Fingerprint({
parameters:[ parameters:[
@ -50,6 +53,16 @@ app.post("/:roomId/join", (request, response) => {
}); });
}); });
const listener = app.listen(process.env.PORT, () => { server.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port); console.log("Your app is listening on port " + server.address().port);
}); });
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
console.log('received: %s', message);
});
ws.send('Connected');
});

View File

@ -3,6 +3,7 @@ dependencies:
express: 4.17.1 express: 4.17.1
express-fingerprint: 1.1.3 express-fingerprint: 1.1.3
express-handlebars: 4.0.3 express-handlebars: 4.0.3
ws: 7.2.3
packages: packages:
/accepts/1.3.7: /accepts/1.3.7:
dependencies: dependencies:
@ -705,6 +706,15 @@ packages:
dev: false dev: false
resolution: resolution:
integrity: sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= integrity: sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
/ws/7.2.3:
dev: false
engines:
node: '>=8.3.0'
peerDependencies:
bufferutil: ^4.0.1
utf-8-validate: ^5.0.2
resolution:
integrity: sha512-HTDl9G9hbkNDk98naoR/cHDws7+EyYMOdL1BmjsZXRUjf7d+MficC4B7HLUPlSiho0vg+CWKrGIt/VJBd1xunQ==
/yallist/2.1.2: /yallist/2.1.2:
dev: false dev: false
resolution: resolution:
@ -724,3 +734,4 @@ specifiers:
express: ^4.17.1 express: ^4.17.1
express-fingerprint: ^1.1.3 express-fingerprint: ^1.1.3
express-handlebars: ^4.0.3 express-handlebars: ^4.0.3
ws: ^7.2.3

View File

@ -22,5 +22,21 @@
BUZZ BUZZ
</button> </button>
</main> </main>
<script>
let socket = new WebSocket(`wss://${window.location.hostname}`);
socket.onopen = function(e) {
socket.send("My name is John");
};
socket.onmessage = function(event) {
a
};
socket.onerror = function(error) {
alert(`[error] ${error.message}`);
};
</script>
</body> </body>
</html> </html>