Added input port

This commit is contained in:
clerie 2018-04-03 14:35:01 +02:00
parent 22bcc7df74
commit 265dbe7ada
9 changed files with 154 additions and 14 deletions

View File

@ -1,31 +1,48 @@
// import all dependencies
const express = require('express'); const express = require('express');
const app = express(); const app = express();
const http = require('http').Server(app); const http = require('http').Server(app);
const io = require('socket.io')(http); 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')); 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){ // input socket
console.log(socket); var server = net.createServer();
socket.on('input', function(data) {
console.log(data); 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(" "); var command = data.split(" ");
if (command.lenght() == 4) { if (command.length == 4) {
if (command[0] == "PX") { if (command[0] == "PX") {
var x = parseInt(command[1]); var x = parseInt(command[1]);
var y = parseInt(command[2]); var y = parseInt(command[2]);
if (!isNaN(x) && !isNaN(y)) { if (!isNaN(x) && !isNaN(y)) {
var color = command[3] 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));
// start input socket
http.listen(port, () => console.log('listening on port ' + port)); server.listen(input_port, () => console.log('[INPUT] port: ' + input_port));

19
node_modules/net/LICENSE generated vendored Normal file
View File

@ -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.

0
node_modules/net/README.md generated vendored Normal file
View File

28
node_modules/net/index.js generated vendored Normal file
View File

@ -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]

54
node_modules/net/package.json generated vendored Normal file
View File

@ -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"
}

6
node_modules/net/test.js generated vendored Normal file
View File

@ -0,0 +1,6 @@
var i = require("./index.js")
console.log(Socket instanceof Function)

5
package-lock.json generated
View File

@ -385,6 +385,11 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
"integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=" "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": { "object-component": {
"version": "0.0.3", "version": "0.0.3",
"resolved": "https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz", "resolved": "https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz",

View File

@ -4,6 +4,7 @@
"description": "Web based Pixelflut server in node.js and socket.io", "description": "Web based Pixelflut server in node.js and socket.io",
"dependencies": { "dependencies": {
"express": "^4.15.2", "express": "^4.15.2",
"net": "^1.0.2",
"socket.io": "^2.1.0" "socket.io": "^2.1.0"
} }
} }

View File

@ -6,11 +6,21 @@
var canvas = document.getElementsByClassName('display')[0]; var canvas = document.getElementsByClassName('display')[0];
var context = canvas.getContext('2d'); var context = canvas.getContext('2d');
var canvas_size_x = 100;
var canvas_size_y = 100;
socket.on('setting', onSetting);
socket.on('draw', onDraw); socket.on('draw', onDraw);
window.addEventListener('resize', onResize, false); window.addEventListener('resize', onResize, false);
onResize(); 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){ function drawLine(x0, y0, x1, y1, color){
context.beginPath(); context.beginPath();
@ -23,15 +33,15 @@
} }
function drawPixel(x, y, color){ function drawPixel(x, y, color){
context.fillStyle = '#' + 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.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){ function onDraw(data){
var w = canvas.width; var w = canvas.width;
var h = canvas.height; var h = canvas.height;
if (data.type == 'pixel') { 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);
} }
} }