1

我用socket.io制作了一个应用程序。我的问题是当我关闭节点并再次打开服务器响应计数时。第一次1 resutlset发送但第二次2和第三次3等等?问题是什么

客户端代码是

<script>
var socket = io.connect('http://10.0.0.192:8888');

socket.on('connecting', function () {
    console.log('connecting');
});

socket.on('connect', function(s){
    console.log('connect');
    socket.emit('Baglan');
    console.log('emit-Baglan');
    socket.on('guncelle',function(data){
        console.log(new Date().getMilliseconds());
        console.dir(data);
    });
});

socket.on('reconnecting', function () {
    console.log('reconnecting');
});

socket.on('reconnect', function () {
    console.log('reconnect');
});

socket.on('reconnect_failed', function () {
    console.log('reconnect_failed');
});
</script>

和服务器

function getDataForClients() {
var d = new Array();
d.push({records:res});
//console.log(d);
return d;}
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    //console.log("Request for " + pathname + " received.");
    route(handle, pathname, response, request);
}
server = http.createServer(onRequest);
io = require('socket.io').listen(server);
io.set('log level', 1);
io.sockets.on('connection', function (client) {
    //console.log(client);
    client.on("Baglan",function(){
        //console.log("user connected");
        __sockets.push(client);
        client.room="weather";
        client.records=[];
        client.join(client.room);
        if(!res)
            guncelle(false,client);
        else
            client.emit("guncelle",getDataForClients());
    });
    client.on('disconnect', function(){
        var i = __sockets.indexOf(client);
        __sockets.splice(i,1);
        client.leave(client.room);
        //console.log("user leave");
    }); 
});

server.listen(8888);
  function guncelle(v,c) {
//console.log("update");
var db = mysql.createClient({
    user: 'user',
    password: '***',
});
db.query('USE '+TEST_DATABASE);
db.query(
    "select * from table",
    function selectCb(err, results, fields) {
        if (err) {
            throw err;
        }
        res = results;
        var _data = getDataForClients();
        if(v)
            io.sockets.emit("guncelle",_data);
        else
            c.emit("guncelle",_data);
        db.end();
    }
);

}

15 毫秒之间有 5 个结果。

抱歉,我无法发布图片。

4

0 回答 0