0

我一直在玩 socket.io,它似乎运行良好。不过最近,我安装了 postgreSQL,以便在每次事件发生时向数据库插入行(并且事件每秒发生 2 或 3 次!)......这是一个片段:

pg.connect(conString, function(err, dbClient) {
  io.sockets.on("connection", function(client) {
    client.on("clientSendingPlayerData", function(playerData) {
      dbClient.query("insert into actions values (1)", function() {
        console.log("INSERTED ROW");
      });
    });
  });
});

我们得到错误:

net.js:391
    throw new Error('Socket is not writable');
          ^
Error: Socket is not writable
    at Socket._writeOut (net.js:391:11)
    at Socket.write (net.js:377:17)
    at [object Object].query (/homes/jjl310/node_modules/pg/lib/connection.js:109:15)
    at [object Object].submit (/homes/jjl310/node_modules/pg/lib/query.js:99:16)
    at [object Object]._pulseQueryQueue (/homes/jjl310/node_modules/pg/lib/client.js:166:24)
    at [object Object].query (/homes/jjl310/node_modules/pg/lib/client.js:193:8)
    at Socket.<anonymous> (/homes/jjl310/tradersgame/server.js:182:16)
    at Socket.$emit (events.js:64:17)
    at SocketNamespace.handlePacket (/homes/jjl310/node_modules/socket.io/lib/namespace.js:335:22)
    at Manager.onClientMessage (/homes/jjl310/node_modules/socket.io/lib/manager.js:469:38)

关于导致问题的原因以及如何解决问题的任何想法?

我将以下库用于 postrgreSQL https://github.com/brianc/node-postgres

4

1 回答 1

2

pg.connect()从池中获取连接,dbClient是一个活动连接。长时间运行的连接可能会超时、出错或因不活动而被丢弃。你想进入pg.connect()回调clientSendingPlayerData。这样,数据库连接仅在需要时才从池中提取,并在完成后返回池中。

于 2012-06-05T16:42:29.973 回答