抛出异常时,我的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' });
}
});
```