Node, server send information to page (bank password control, lab ...)

0

Good evening!

I do not know if it is possible to implement the following situation: I have a html page that is a monitor (has the last number called), on the second page html, the user can click to request the next number. I can even exchange information via socket, but only with the page that was clicked, I would have to update the other page that is the monitor. If you can not do this, you would have to check the endpoint at any time to see if the number has changed. Any suggestions, even if it's not via socket?

Current solution that did not work:

server.js

var http = require('http')
var express = require('express')
var socketio = require('socket.io')
var app = express(server)
var server = http.Server(app)
var io = socketio(server)

app.get('/', function(req, res){
  console.log('Passei aqui!!!');

  req.socket.on('login_monitor', function(data, callback) {
    req.socket.join('monitor');
    callback();
   });

  res.sendFile(__dirname + '/monitor.html');
});

app.get('/teste', function(req, res){
  console.log('TESTE!!!');
  io.on('connection', function(socket){
    socket.on('echo', function(data){
      socket.emit('echo', 'Servidor--->');
      console.log(data);

       socket.on('echo', function(data) {
        socket.emit('echo', 'Servidor--->'); //envia para quem invocou o echo
        io.to('monitor').emit('echo', 'Funcionou!!!!!'); //envia para o monitor
      });

    });
  });
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('echo', function(data){
    socket.emit('echo', 'Servidor--->');
    console.log(data);
  });
});

server.listen(8080); 

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>Echo server</title>

    <script src="socket.io/socket.io.js"></script>
    <script type="text/javascript">
      var socket = io();
      socket.on('echo', function(data){
        console.log(data);
      });
      socket.emit('echo', 'this is a message');
    </script>
  </head>
  <body>
    <h1>Próximo cliente (Clique aqui)</h1>
  </body>
</html>

monitor.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>Echo server</title>

    <script src="socket.io/socket.io.js"></script>
    <script type="text/javascript">
      var socket = io();
      socket.on('echo', function(data){
        console.log(data);
      });
      socket.emit('login_monitor', 'this is a message', function(){
        console.log('logado como monitor');
      });
    </script>
  </head>
  <body>
    <h1>Senha 609 caixa 3</h1>
  </body>
</html>
    
asked by anonymous 04.11.2018 / 06:04

1 answer

1

Create a login to the monitor

monitor.html

  socket.emit('login_monitor', {/*envie parâmetros se necessário*/}, function(){
      console.log('logado como monitor');
  });

server.js

  socket.on('login_monitor', function(data, callback) {
      socket.join('monitor');
      callback();
  });

Now that monitor sockets can be identified with the 'monitor' key, you can send messages to them in echo

socket.on('echo', function(data) {
  socket.emit('echo', 'Servidor--->'); //envia para quem invocou o echo
  io.to('monitor').emit('echo', 'Servidor--->'); //envia para o monitor
});
    
04.11.2018 / 06:45