的用mysql节点JS和socket.io在客户记录着更新列表
大家好,我希望在新的记录插入到mysql数据库,使用节点js和socket.io即时更新客户端。
我该怎么办这个问题,即时通讯新的节点js和插座原谅我,如果我的代码是太混乱了。我搜索了很多,但没有发现任何东西。任何建议将是有益的。这里是我的server.js文件:
var mysql = require('mysql') ;
var io = require('socket.io').listen(3000) ;
var db = mysql.createConnection({
host:'localhost',
user:'root',
database:'mygame'
});
db.connect(function(err){
if(err) console.log(err);
console.log("Connected Successfully To The Database");
});
// Here We Need List Of Competitions To Emit To All Clients
var Competitions = [] ;
var isInitCompetitions = false ;
var socketCount = 0 ;
//
io.sockets.on('connection',function(socket){
Competitions = [];
db.query("SELECT * FROM comps WHERE compstatus='1'").on(
'result',function(data) {
Competitions.push(data) ;
}
).on(
'end',function(){
socket.emit('all competitions',Competitions);
}
) ;
socketCount++ ;
io.sockets.emit('Another User Is Connected : ' ,socketCount);
console.log('One User Is Connected');
socket.on('disconnect',function() {
io.sockets.emit('One User Left : ' ,socketCount);
});
function checkNewCompetitions() {
socket.on('newcomp',function(){
db.query("SELECT * FROM comps WHERE compstatus='1'").on(
'result',function(data) {
Competitions = [];
Competitions.push(data) ;
}
).on(
'end',function(){
socket.emit('all competitions',Competitions);
console.log('Number Of Competitions in Flow : ' , Competitions.length) ;
}
) ;
});
}
setInterval(checkNewCompetitions,3000);
});
这里是我的index.html文件:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
$(document).ready(function() {
var socket = io.connect('http://localhost:3000');
socket.on('all competitions',function(data) {
var html = '' ;
for(var i = 0 ;i < data.length; i++){
html += '<li>'+data[i].id + '</li>';
}
$('#competitions').html(html);
});
socket.on('newcomp',function(data) {
var html = '' ;
for (let index = 0; index < data.length; index++) {
const element = data[index];
html += '<li>' + data[index].id + '</li>';
}
$('#competitions').html(html);
});
});
</script>
<p>This is List Of Competitions</p>
<ul id="competitions"></ul>
我运行此代码,但我得到这个错误:
(node:8628) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 newcomp listeners added. Use emitter.setMaxListeners() to increase limit
我可以为这个问题做些什么,感谢这个马丽娟社区。
每3秒所以MaxListenersExceededWarning出现你添加一个新的事件监听器。每3秒召唤checkNewCompitions是确定的。如果有一个新的压缩机,你必须发出与socket.emit一个newComp事件(“newcomp”)
socketCount++ ;
io.sockets.emit('Another User Is Connected : ' ,socketCount);
console.log('One User Is Connected');
socket.on('disconnect',function() {
io.sockets.emit('One User Left : ' ,socketCount);
});
function checkNewCompetitions() {
/// Every 3 seconds you check the database for new comps
db.query("SELECT * FROM comps WHERE compstatus='1'").on(
'result',function(data) {
Competitions = [];
Competitions.push(data) ;
}
).on(
'end',function(){
// here you have to call socket.emit('newComp')
// so the client gets the event a new comp is inserted in database.
socket.emit('all competitions',Competitions);
console.log('Number Of Competitions in Flow : ' , Competitions.length) ;
}
) ;
}
setInterval(checkNewCompetitions,3000);
});
我不能找到你发出“newcomp”事件。所以socket.on(“newcomp” ..)从来没有被调用