From 265dbe7ada94bfdd9ecdec2b46e1bb9df6262dc9 Mon Sep 17 00:00:00 2001 From: clerie Date: Tue, 3 Apr 2018 14:35:01 +0200 Subject: [PATCH] Added input port --- index.js | 39 ++++++++++++++++++------- node_modules/net/LICENSE | 19 ++++++++++++ node_modules/net/README.md | 0 node_modules/net/index.js | 28 ++++++++++++++++++ node_modules/net/package.json | 54 +++++++++++++++++++++++++++++++++++ node_modules/net/test.js | 6 ++++ package-lock.json | 5 ++++ package.json | 1 + public/main.js | 16 +++++++++-- 9 files changed, 154 insertions(+), 14 deletions(-) create mode 100644 node_modules/net/LICENSE create mode 100644 node_modules/net/README.md create mode 100644 node_modules/net/index.js create mode 100644 node_modules/net/package.json create mode 100644 node_modules/net/test.js diff --git a/index.js b/index.js index e35d4c6..5a4421f 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,48 @@ +// import all dependencies const express = require('express'); const app = express(); const http = require('http').Server(app); const io = require('socket.io')(http); -const port = process.env.PORT || 61813; +const http_port = process.env.PORT || 61813; +const net = require('net'); +const input_port = 1337; +// define canvas +var canvas_size_x = 100; +var canvas_size_y = 100; + +// define static folder app.use(express.static(__dirname + '/public')); +// output socket +io.on('connection', function (socket){ + console.log("[HTTP] new connection"); + socket.emit('setting', {canvas: {size: {x: canvas_size_x, y: canvas_size_y}}}); +}); -function onConnection(socket){ - console.log(socket); - socket.on('input', function(data) { - console.log(data); +// input socket +var server = net.createServer(); + +server.on('connection', function (server_socket) { + server_socket.setEncoding('utf8'); + console.log('[INPUT] new input'); + server_socket.on('data', function (data) { var command = data.split(" "); - if (command.lenght() == 4) { + if (command.length == 4) { if (command[0] == "PX") { var x = parseInt(command[1]); var y = parseInt(command[2]); if (!isNaN(x) && !isNaN(y)) { var color = command[3] - socket.broadcast.emit('draw', {type: 'pixel', x: x, y: y, color: color}); + io.sockets.emit('draw', {type: 'pixel', x: x, y: y, color: color}); } } } }); -} +}); -io.on('connection', onConnection); +// start http +http.listen(http_port, () => console.log('[HTTP] port: ' + http_port)); - -http.listen(port, () => console.log('listening on port ' + port)); +// start input socket +server.listen(input_port, () => console.log('[INPUT] port: ' + input_port)); diff --git a/node_modules/net/LICENSE b/node_modules/net/LICENSE new file mode 100644 index 0000000..8952fa5 --- /dev/null +++ b/node_modules/net/LICENSE @@ -0,0 +1,19 @@ +Copyright 2011 Sleepless Software Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/node_modules/net/README.md b/node_modules/net/README.md new file mode 100644 index 0000000..e69de29 diff --git a/node_modules/net/index.js b/node_modules/net/index.js new file mode 100644 index 0000000..7120071 --- /dev/null +++ b/node_modules/net/index.js @@ -0,0 +1,28 @@ +/* +Copyright 2013 Sleepless Software Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. +*/ + +// yes, I know this seems stupid, but I have my reasons. + +var net = require("net") +for(k in net) + global[k] = net[k] + diff --git a/node_modules/net/package.json b/node_modules/net/package.json new file mode 100644 index 0000000..4f7434c --- /dev/null +++ b/node_modules/net/package.json @@ -0,0 +1,54 @@ +{ + "_from": "net", + "_id": "net@1.0.2", + "_inBundle": false, + "_integrity": "sha1-0XV+yaf7I3HYPPR1XOPifhCCk4g=", + "_location": "/net", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "net", + "name": "net", + "escapedName": "net", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/net/-/net-1.0.2.tgz", + "_shasum": "d1757ec9a7fb2371d83cf4755ce3e27e10829388", + "_spec": "net", + "_where": "/home/clemens/Dokumente/git/pixelnode", + "author": { + "name": "Joe Hitchens", + "email": "joe@sleepless.com", + "url": "sleepless.com" + }, + "bugs": { + "url": "https://github.com/sleeplessinc/net/issues" + }, + "bundleDependencies": false, + "deprecated": false, + "description": "Globalizes the 'net' module functions", + "homepage": "https://github.com/sleeplessinc/net#readme", + "license": { + "type": "MIT", + "url": "https://github.com/sleeplessinc/net/raw/master/LICENSE" + }, + "name": "net", + "repositories": [ + { + "type": "git", + "url": "git+https://sleeplessinc@github.com/sleeplessinc/net.git" + } + ], + "repository": { + "type": "git", + "url": "git+https://sleeplessinc@github.com/sleeplessinc/net.git" + }, + "version": "1.0.2" +} diff --git a/node_modules/net/test.js b/node_modules/net/test.js new file mode 100644 index 0000000..b6a7040 --- /dev/null +++ b/node_modules/net/test.js @@ -0,0 +1,6 @@ + +var i = require("./index.js") + +console.log(Socket instanceof Function) + + diff --git a/package-lock.json b/package-lock.json index 8d28f00..57c6503 100644 --- a/package-lock.json +++ b/package-lock.json @@ -385,6 +385,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=" }, + "net": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/net/-/net-1.0.2.tgz", + "integrity": "sha1-0XV+yaf7I3HYPPR1XOPifhCCk4g=" + }, "object-component": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz", diff --git a/package.json b/package.json index 44371c0..4d1d43f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Web based Pixelflut server in node.js and socket.io", "dependencies": { "express": "^4.15.2", + "net": "^1.0.2", "socket.io": "^2.1.0" } } diff --git a/public/main.js b/public/main.js index f147c29..8b024d8 100644 --- a/public/main.js +++ b/public/main.js @@ -6,11 +6,21 @@ var canvas = document.getElementsByClassName('display')[0]; var context = canvas.getContext('2d'); + var canvas_size_x = 100; + var canvas_size_y = 100; + + socket.on('setting', onSetting); + socket.on('draw', onDraw); window.addEventListener('resize', onResize, false); onResize(); + function onSetting(data) { + canvas_size_x = data.canvas.size.x; + canvas_size_y = data.canvas.size.y; + } + function drawLine(x0, y0, x1, y1, color){ context.beginPath(); @@ -23,15 +33,15 @@ } function drawPixel(x, y, color){ - context.fillStyle = '#' + color; - context.fillRect(x - (canvas.width * 0.005),y - (canvas.height * 0.005), canvas.width * 0.01, canvas.height * 0.01); + context.fillStyle = color; + context.fillRect(x - (canvas.width * (0.5/canvas_size_x)),y - (canvas.height * (0.5/canvas_size_y)), canvas.width * (1/canvas_size_x), canvas.height * (1/canvas_size_y)); } function onDraw(data){ var w = canvas.width; var h = canvas.height; if (data.type == 'pixel') { - drawPixel(data.x * w, data.y * h, data.color); + drawPixel((data.x / canvas_size_x) * w, (data.y / canvas_size_y) * h, data.color); } }