无法使用mysql节点js和socket.io更新客户端中的记录列表
嗨,大家好,我想在将新记录插入mysql数据库中时使用node js和socket.io更新客户端。
对于这个问题,我该怎么办,如果我的代码太混乱了,对Node js和socket来说我是原谅。 我搜索了很多,但没有找到任何东西。 任何建议都会有所帮助。 这是我的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
感谢这个伟大的社区,我该怎么办?
答案 0 :(得分:2)
每3秒钟添加一个新的事件侦听器,以便发生MaxListenersExceededWarning。每3秒调用一次checkNewCompitions是可以的。如果有新的comp,则必须使用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'..)永远不会被调用