w12_node exercise
This commit is contained in:
parent
8827db575b
commit
8a67292ac0
1
w12_node/save-dots/client/.gitignore
vendored
Normal file
1
w12_node/save-dots/client/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
.vscode/**
|
24
w12_node/save-dots/client/index.html
Normal file
24
w12_node/save-dots/client/index.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.3/p5.min.js"
|
||||
integrity="sha512-I0Pwwz3PPNQkWes+rcSoQqikKFfRmTfGQrcNzZbm8ALaUyJuFdyRinl805shE8xT6iEWsWgvRxdXb3yhQNXKoA=="
|
||||
crossorigin="anonymous"
|
||||
></script>
|
||||
|
||||
<script src="https://cdn.socket.io/3.1.3/socket.io.min.js"></script>
|
||||
|
||||
<script src="sketch.js"></script>
|
||||
|
||||
<meta charset="utf-8" />
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body></body>
|
||||
</html>
|
38
w12_node/save-dots/client/sketch.js
Normal file
38
w12_node/save-dots/client/sketch.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
const socket = io('localhost:3000');
|
||||
|
||||
const SIZE = 10;
|
||||
let coord = [];
|
||||
|
||||
socket.on("connect", () => {
|
||||
console.log('Connected');
|
||||
});
|
||||
|
||||
socket.on('data', (data) => {
|
||||
coord = data
|
||||
});
|
||||
|
||||
function setup() {
|
||||
createCanvas(800, 600);
|
||||
|
||||
createButton('Save').position(0, 0).mousePressed( () => {
|
||||
socket.emit('save', coord);
|
||||
});
|
||||
|
||||
socket.emit('load', coord);
|
||||
}
|
||||
|
||||
|
||||
|
||||
function draw() {
|
||||
background(0);
|
||||
fill(255);
|
||||
noStroke();
|
||||
if (coord.length == 0) return;
|
||||
for (let {x,y} of coord){
|
||||
ellipse(x, y, SIZE, SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
function mousePressed(){
|
||||
coord.push ({x:mouseX, y:mouseY});
|
||||
}
|
2
w12_node/save-dots/server/.gitignore
vendored
Normal file
2
w12_node/save-dots/server/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
node_modules/**
|
||||
.vscode/**
|
1
w12_node/save-dots/server/coordinates.json
Normal file
1
w12_node/save-dots/server/coordinates.json
Normal file
|
@ -0,0 +1 @@
|
|||
[]
|
34
w12_node/save-dots/server/index.js
Normal file
34
w12_node/save-dots/server/index.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
import path from 'path';
|
||||
import express from 'express';
|
||||
import fs from 'fs';
|
||||
import { createServer } from 'http';
|
||||
import { Server } from 'socket.io';
|
||||
// Create a server on port 3000
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
// socket.io
|
||||
|
||||
const io = new Server(server, {
|
||||
cors: {
|
||||
origin: '*',
|
||||
},
|
||||
});
|
||||
|
||||
server.listen(PORT, function () {
|
||||
console.log('Server listening at port %d', PORT);
|
||||
});
|
||||
|
||||
// Optionally routing your app
|
||||
// app.use(express.static(path.join(__dirname, '../client')));
|
||||
|
||||
io.on('connection', function (socket) {
|
||||
socket.on('save', function (data) {
|
||||
//...
|
||||
});
|
||||
|
||||
socket.on('load', function () {
|
||||
///...
|
||||
});
|
||||
});
|
1152
w12_node/save-dots/server/package-lock.json
generated
Normal file
1152
w12_node/save-dots/server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
w12_node/save-dots/server/package.json
Normal file
18
w12_node/save-dots/server/package.json
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "save-dots-example",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"express": "^5.1.0",
|
||||
"socket.io": "^4.8.1"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user