1

I'm have 2 websites hosted on localhost with different ports and I'm trying to make first one only to send data and second one only to receive that data. Earlier, I made that happens with only one website. User could login, and logged users could change data and that data would be emitted to all other users. Users which aren't logged could just view page. But there were so many problems. Taking that a part would fix some problems. I'm trying to do all this without back-end experience and knowledge just watching some videos and pasting code, and I'm sorry about that, I know this is wrong. I figured out some things but I can't make it work. So here is my terrible code:

This is node.js code for website which will send data

var express = require("express");

var app = express();
var server = app.listen(3000);
app.use(express.static('public'));
var socket = require("socket.io");
var io = socket(server);
io.sockets.on('connection', newConnection);

function newConnection(socket)
{
  console.log('Connection: ' + socket.id);
  socket.on('data', dataMsg);
  function dataMsg(data)
  {
    socket.broadcast.emit('data', data);
  }
}

This is javascript code for website which will send data which works fine Btw functions press1(), press2() etc. are called from html by clicking certain buttons

let ps = 2.5;
let bl = 3.5;
const socket = io('http://localhost:3000');
function update(data)
{
  buttons[0].time = data.btn11;
  buttons[0].price = data.btn12;
  buttons[1].time = data.btn21;
  buttons[1].price = data.btn22;
  buttons[2].time = data.btn31;
  buttons[2].price = data.btn32;
  buttons[3].time = data.btn41;
  buttons[3].price = data.btn42;
}

let buttons = [];
buttons[0] = new Button(ps);
buttons[1] = new Button(ps);
buttons[2] = new Button(ps);
buttons[3] = new Button(bl);


let arr = [];
arr[0] = [document.getElementById("prviprvi"), document.getElementById('prvidrugi')];
arr[1] = [document.getElementById('drugiprvi'), document.getElementById('drugidrugi')];
arr[2] = [document.getElementById('treciprvi'), document.getElementById('trecidrugi')];
arr[3] = [document.getElementById('cetvrtiprvi'), document.getElementById('cetvrtidrugi')];

var tempD = setInterval( () => {
  var data = {
    btn11 : buttons[0].time,
    btn12 : buttons[0].price,
    btn21 : buttons[1].time,
    btn22 : buttons[1].price,
    btn31 : buttons[2].time,
    btn32 : buttons[2].price,
    btn41 : buttons[3].time,
    btn42 : buttons[3].price
  }
  socket.emit("data", data);
  for(var i = 0; i < 4; i++)
  {
    buttons[i].update();
    buttons[i].show(arr[i][0], arr[i][1]);
  }
},100);

function press1()
{
  buttons[0].press();
}

function press2()
{
  buttons[1].press();
}

function press3()
{
  buttons[2].press();
}

function press4()
{
  buttons[3].press();
}


function Button(tempD1)
{
  this.val = tempD1;
  this.pressed = false;
  this.startingTime = Date.now();
  this.endingTime = Date.now();
  this.hours = 0;
  this.min = 0;
  this.sec = 0;
  this.price = 0;
  this.time = "0:0:0";

  this.show = function(span1, span2)
  {
    span1.innerHTML = this.time;
    span2.innerHTML = this.price + " Din";
  }

  this.update = function()
  {
    if(this.pressed)
    {
      this.endingTime = Date.now();
      this.tempD = this.endingTime - this.startingTime;
      this.sec = parseInt((this.tempD / 1000) % 60);
      this.min = parseInt(((this.tempD / 1000) / 60) % 60);
      this.hours = parseInt((((this.tempD / 1000) / 60) / 60) % 24);
      this.time = this.hours + ":" + this.min + ":" + this.sec;
      this.price = this.hours * 60 * this.val + this.min * this.val;
    }
  }

  this.press = function()
  {
    if(this.pressed == false)
    {
      this.startingTime = Date.now();
      this.pressed = true;
    }
    else
    {
      this.pressed = false;
    }
  }
}

This is node.js for website which receiving data

  var express = require("express");

    var app = express();
    var server = app.listen(3001);
    app.use(express.static('public'));
    var socket = require("socket.io");
    var io = socket('localhost:3000');
    io.sockets.on('connection', newConnection);

    function newConnection(socket)
    {
      console.log('Connection: ' + socket.id);
      socket.on('data', dataMsg);
      function dataMsg(data)
      {
        socket.broadcast.emit('data', data);
      }
    }

This is javascript code for website which receiving data

let ps = 2.5;
let bl = 3.5;
const socket = io('http://localhost:3000');
socket.on('data', update);
function update(data)
{
  buttons[0].time = data.btn11;
  buttons[0].price = data.btn12;
  buttons[1].time = data.btn21;
  buttons[1].price = data.btn22;
  buttons[2].time = data.btn31;
  buttons[2].price = data.btn32;
  buttons[3].time = data.btn41;
  buttons[3].price = data.btn42;
}

var tempD = setInterval( () => {
  for(var i = 0; i < 4; i++)
    buttons[i].show(arr[i][0], arr[i][1]);
}
let buttons = [];
buttons[0] = new Button(ps);
buttons[1] = new Button(ps);
buttons[2] = new Button(ps);
buttons[3] = new Button(bl);

let arr = [];
arr[0] = [document.getElementById("prviprvi"), document.getElementById('prvidrugi')];
arr[1] = [document.getElementById('drugiprvi'), document.getElementById('drugidrugi')];
arr[2] = [document.getElementById('treciprvi'), document.getElementById('trecidrugi')];
arr[3] = [document.getElementById('cetvrtiprvi'), document.getElementById('cetvrtidrugi')];

function Button(tempD1)
{
  this.val = tempD1;
  this.pressed = false;
  this.startingTime = Date.now();
  this.endingTime = Date.now();
  this.hours = 0;
  this.min = 0;
  this.sec = 0;
  this.price = 0;
  this.time = "0:0:0";

  this.show = function(span1, span2)
  {
    span1.innerHTML = this.time;
    span2.innerHTML = this.price + " Din";
  }

  this.update = function()
  {
    if(this.pressed)
    {
      this.endingTime = Date.now();
      this.tempD = this.endingTime - this.startingTime;
      this.sec = parseInt((this.tempD / 1000) % 60);
      this.min = parseInt(((this.tempD / 1000) / 60) % 60);
      this.hours = parseInt((((this.tempD / 1000) / 60) / 60) % 24);
      this.time = this.hours + ":" + this.min + ":" + this.sec;
      this.price = this.hours * 60 * this.val + this.min * this.val;
    }
  }

  this.press = function()
  {
    if(this.pressed == false)
    {
      this.startingTime = Date.now();
      this.pressed = true;
    }
    else
    {
      this.pressed = false;
    }
  }
}

Asking this may be too much but I will do it anyway: How to allow access to the one which sends data with just the right password. When user go to website he will be asked for password and if he type right one, he will be forwarded further. I did it last time in javascript but anyone could just go to inspect elements and see password

Thank you!

Aaras
  • 11
  • 3
  • See if this help: https://stackoverflow.com/questions/30013032/prevent-user-to-find-password-through-firebug-chrome-dev-tools – Usman Afzal Dec 30 '19 at 19:47

0 Answers0