我一直在玩 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)
关于导致问题的原因以及如何解决它的任何想法?
我正在使用以下库进行postrgreSQLhttps://github.com/brianc/node-postgres
pg.connect()
从池中获取连接,dbClient
是活动连接。长时间运行的连接可能会超时、出错或因不活动而被丢弃。您希望将pg.connect()
移入clientSendingPlayerData
回调。这样,数据库连接仅在需要时从池中拉取,并在完成后返回到池中。