簡體   English   中英

節點JS-SyntaxError:意外令牌

[英]Node JS - SyntaxError: 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');
      });
    });

這是一個用node編寫的簡單的聊天程序,它允許多個客戶端相互連接和聊天。

您在此行的Object.entries(sockets).forEach(([key, cs]) =>{})添加了一個括號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM