SocketIO在发生异常时崩溃-如何捕获并继续运行?

时间:2018-08-21 22:38:08

标签: socket.io

抛出异常时,我的SocketIO服务器崩溃。

如何捕获异常或仅使套接字崩溃?

每个try/catch处理程序中都需要一个on()吗?

谢谢!

```
io.on('connection', (socket) => {
  // These variables are attached to the socket and used in the event code below
  socket.myGame; // your game
  socket.id;  // your playerId
  debug(`Socket '${socket.id}' connected`);

  // Create a new game
  // This creates a game, but doesn't join the player.
  socket.on('createGame', (fn) => {
    try {
      var g = GM.createGame(socket); // <-- throws
      fn({ data: g.id });
    } catch(e) {
      debug('ERROR createGame', e);
      fn({ data: 'error' });
    }
  });
```

0 个答案:

没有答案