【发布时间】:2016-01-18 01:35:23
【问题描述】:
每次页面更新后,我都有 +1 套接字连接..
module.exports = function(io, client) {
var GameController = {
gamePage: function(req, res) {
client.hget('games', 'game.' + req.params.id, function (err, result) {
if (err) return result(err);
var game = JSON.parse(result);
io.on('connection', function (socket) {
console.log('send');
console.log(socket.id);
io.emit('get_bets', game.players);
});
res.render('pages/game', {
title: 'Game - ' + req.params.id,
user: req.user,
game: game
});
});
};
return GameController;
});
路由文件:
module.exports = function(io, client) {
var express = require('express');
var router = express.Router();
var GameController = require('controllers/GameController')(io, client);
router.get('/:id', GameController.gamePage);
...
return router;
};
客户端反应:
var Game = React.createClass({
getInitialState: function() {
this.socket = io();
return {
bets: null
}
},
socketGetBets: function() {
var that = this;
this.socket.on('get_bets', function(data) {
console.log('get bets');
that.setState({ bets: data });
});
this.socket.on('rand', function(data) {
console.log(data);
});
},
...
但调试后我发现什么问题不在客户端。
app.js 文件:
var socket_io = require('socket.io');
var io = socket_io();
app.io = io;
//route
var game = require('./routes/games')(io, client);
bin/www 文件:
var server = http.createServer(app);
var io = app.io;
io.attach( server );
页面更新后,io.on("connection") 事件在控制台中显示“send”消息,第二次页面更新后,我有“send”“send”,第三次更新 - “send”“send”“send ”等比出现内存泄漏警告。控制台日志 socked.id 多次显示相同的值。
【问题讨论】:
标签: node.js express reactjs socket.io