0

I'm trying to figure out why I'm getting this unexpected token .

 Object.entries(sockets).forEach(([key, cs])) => {
     ^

SyntaxError: Unexpected token .
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:607:28)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3

Object.entries(sockets).forEach(([key, cs])) => {

    process.stdout.write('\u001B[2J\u001B[0;0f'); //again clear node terminal
    const server = require('net').createServer(); // here use the createServer method from net module
    let counter = 0;
    let sockets = {};

    server.on('connection', socket => {
      socket.id = counter++; //everytime a client connects assign an id, and add to the counter for next socket Id

      //sockets[sockets.id] = socket;

      console.log('Client is connected!');
      socket.write('Hi, your name?\n');

      socket.on('data', data => {
        if(!socket[socket.id]) {
          socket.name = data.toString().trim();
          socket.write(`Welcome ... $[socket.name}!\n`);
          sockets[socket.id] = socket;
          return;
        }
       Object.entries(sockets).forEach(([key, cs])) => {
        if (socket.id == key)
          return;
        cs.write(`${socket.name}: `);
      //  console.log('data is', data);
        cs.write(data);
      });
    });

      socket.on('end', () => {
        delete sockets[socket.id];
        console.log('Client is now disconnected');
      });
    });

This is a simple chat program written in node that allows multiple clients to connect and chat among each other.

4
  • What version of Node? Commented Mar 23, 2018 at 13:37
  • Object.entries(sockets).forEach(([key, cs]) => {}); There is extra bracket in your code Commented Mar 23, 2018 at 13:46
  • There is not an issue of node version you put extra bracket. Commented Mar 23, 2018 at 13:48
  • @Dipakchavda thank you! It was the extra bracket. I'm using atom editor and my eyes just became sore. It's Node 8. Again, thank you! Commented Mar 23, 2018 at 13:49

1 Answer 1

1

You added an extra parenthesis in Object.entries(sockets).forEach(([key, cs]) =>{}) this line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.