1

我写了一个简单的Node.js WebSocket 聊天服务器。要运行服务器,我正在使用foreman startProcfile包含以下行的 a:chat: npm start

我还编写了一个使用SocketRocket连接到上述服务器的 iPhone 应用程序。在,applicationDidEnterBackground:我打电话closewebSocket而且,在applicationWillEnterForeground:中,我重新创建webSocket并调用open.

进入后台或前台时,iPhone 应用程序似乎使服务器崩溃并出现以下错误:

chat.1 | events.js:74
chat.1 |         throw TypeError('Uncaught, unspecified "error" event.');
chat.1 |               ^
chat.1 | TypeError: Uncaught, unspecified "error" event.
chat.1 |     at TypeError (<anonymous>)
chat.1 |     at WebSocket.EventEmitter.emit (events.js:74:15)
chat.1 |     at Receiver.self._receiver.onerror (~/Projects/Chat/node_modules/ws/lib/WebSocket.js:719:10)
chat.1 |     at Receiver.error (~/Projects/Chat/node_modules/ws/lib/Receiver.js:301:8)
chat.1 |     at Receiver.opcodes.8.finish (~/Projects/Chat/node_modules/ws/lib/Receiver.js:497:14)
chat.1 |     at Receiver.<anonymous> (~/Projects/Chat/node_modules/ws/lib/Receiver.js:478:33)
chat.1 |     at Receiver.add (~/Projects/Chat/node_modules/ws/lib/Receiver.js:93:24)
chat.1 |     at Socket.firstHandler (~/Projects/Chat/node_modules/ws/lib/WebSocket.js:678:22)
chat.1 |     at Socket.EventEmitter.emit (events.js:95:17)
chat.1 |     at Socket.<anonymous> (_stream_readable.js:746:14)
chat.1 | npm ERR! weird error 8
chat.1 | npm ERR! not ok code 0
chat.1 | exited with code 1
system | sending SIGTERM to all processes

为什么会这样?而且,我该如何解决?

4

1 回答 1

3

我昨天遇到了同样的错误,并且能够通过附加“错误”事件处理程序在 Node.Js 端修复它

wss.on('connection', function(connection) {
  connection.on('error', function(reason, code) {
    console.log('socket error: reason ' + reason + ', code ' + code);
  });
}

我将尝试在 SocketRocket 代码中找到导致该问题的原因,但现在它适用于

于 2013-10-24T16:27:55.913 回答