0

I am currently working at a nodeJS robotics app and i am trying to use sockets.io in order to show some messages and data in browser.

I have the following nodeJS code:

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){
  console.log('A user connected');
      socket.on('clientEvent', function(data){
            function take_tool() {
                while(!take_tool_flag) {
                    if(fs.existsSync('D:/flag.txt')) {
                        fs.writeFileSync('D:/input_robot.txt', '-1');
                        socket.emit('testerEvent', 'Robot is taking the writing tool!');
                        console.log('Robot is taking the writing tool!');
                        fs.unlinkSync('D:/flag.txt');
                        take_tool_flag = true;
                    }
                }
            }
            function draw_table() {
                while(!draw_table_flag && take_tool_flag) {
                    if(fs.existsSync('D:/flag.txt')) {
                        fs.writeFileSync('D:/input_robot.txt', '-3');   
                        socket.emit('testerEvent', 'Robot is drawing the table game!');   
                        console.log('Robot is drawing the table game!');
                        fs.unlinkSync('D:/flag.txt');
                        draw_table_flag = true;
                    }
                }
            }
            function game() {

                socket.emit('testerEvent', 'A new game has started!');
                console.log("A new game has started!");

                fs.writeFileSync('D:/input_robot.txt', '-99');

                if(!take_tool_flag) { take_tool(); }

                if(!draw_table_flag) { draw_table(); }
            }

            game();
      });
      socket.on('disconnect', function () {
        console.log('A user disconnected');
      });
});

http.listen(3000, function(){
  console.log('listening on localhost:3000');
});

And then in HTML:

<!DOCTYPE html>
<html>
    <head><title>Hello world</title></head>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io();
      socket.on('testerEvent', function(data){document.write(data)});

      function start() {
        socket.emit('clientEvent', 'Sent an event from the client!');
      }
    </script>
    <body>Hello world
        <button onclick="start()">Start</button>
    </body>
</html>

When i press the start button from html the first 3 instructions from game(); function are executed:

socket.emit('testerEvent', 'A new game has started!');
console.log("A new game has started!");

fs.writeFileSync('D:/input_robot.txt', '-99');

and i get the message in browser from socket.emit;

Then it enters in take_tool(); function where it waits for 'D:/flag.txt' to be created. When i create this file the execution of take_tool(); continues but i don't get in browser the message from socket.emit. Same thing for draw_table(); function. I receive those messages instead at the end of game(); function execution. I need those messages to be shown in browser in real time not at the end of function execution. What seems to be the problem? Thank you!

Codrut Tapu
  • 231
  • 1
  • 2
  • 6

0 Answers0