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

Asked

Viewed 48 times

0

Good night!

I do not know if it is possible to implement the following situation: I have an html page that is a monitor (it has the last number called), in the second html page, 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’t do it that way, you’d have to check the endpoint every second to see if the number’s been changed. Any suggestions, even if 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>

html monitor.

<!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>

1 answer

1


Create a login for the monitor

html monitor.

  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 the monitor sockets can be identified with the 'monitor' key, you can send messages to them on 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
});
  • Thanks for the return, I will make the changes now in the late afternoon. Thanks!!!

  • I’m starting with Node and Javascript, my doubt may be very simple, but by lack of knowledge. I included the code on the.hrml monitor, that code is responsible for identifying the page, right? I changed the server.js in the root endpoint '/' to identify the monitor in the back end. Each '/test' call includes the last snippet of code you sent. I have an error accessing root 0.0.0.0:8080. I will update the codes in question. Thanks for the force.

  • I studied about socket, your answer is show. I couldn’t understand. Thanks!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.