我为我的网站开发了一个即时消息系统,我有两个问题。首先是console.log不起作用,所以我无法跟踪正在执行的脚本。第二个是我无法看到传入的消息,只能看到我输入的消息,并且消息没有添加到数据库中。可能问题在于mysql请求或mysql连接。请帮我解决这些问题
服务器
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'lc',
});
var io = require('socket.io').listen(8080);
/*io.set('log level', 0);*/
io.sockets.on('connection', function (socket) {
function escapeHtml(str) {
return String(str)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, "")
.replace(/'/g, "'")
.replace(/\//g, "⁄");
}
socket.on('connect_user', function(login,password, idUserGet){
var sql = 'SELECT * FROM users WHERE login = ' + connection.escape(login) + ' AND password = ' + connection.escape(password) + ' ';
connection.query(sql, function(err, rows) {
if(rows.length > 0) {
socket.userGet = idUserGet;
socket.userSend = rows[0].id;
console.log(socket.userSend+' has been logined ');
socket.join(socket.userSend);
socket.user = rows[0].login;
}
});
socket.on( 'msg', function(msg) {
// htmlencode
msg = escapeHtml(msg);
// Unixtime
milliseconds = parseInt(new Date().getTime()/1000);
var post = {user_send: socket.userSend, user_get: socket.userGet, text: msg, date: milliseconds};
query = connection.query('INSERT INTO messages SET ?', post, function(err, result) {
update = connection.query(sql);
});
console.log(' usersend id: ' +socket.userSend +' user get: ' + socket.userGet + ' ');
socket.broadcast.to(socket.userGet).json.emit('incMsg',{'userSend': socket.userSend, 'name': socket.user, 'text': msg, 'time': time })
});
});
});
客户端
function tpl(t) {
html = ' <div class="msg>';
html += ' <div class="img"><img src="'+t.img+'" /></div>';
html += ' <div class="info">';
html += ' <div class="name"><a href="#">' + t.name + '</a></div>';
html += ' <div class="text">'+t.text+'</div>';
html += ' </div>';
html += '</div>';
return html;
}
$(window).load(function() {
var milliseconds = parseInt(new Date().getTime()/1000);
if (navigator.userAgent.toLowerCase().indexOf('chrome') != -1) {
socket = io.connect('http://localhost:8080', {'transports': ['xhr-polling']});
} else {
socket = io.connect('http://localhost:8080');
}
socket.on('connect', function () {
var id = '<?=$MY_FRIEND_ID;?>';
img = $("#userGet img").attr('src');
$("#userGet img").attr('src',img);
$("#messages").scrollTop($("#messages")[0].scrollHeight+9999999);
socket.id = id;
socket.emit('adduser', '<?=$MY_LOGIN;?>','<?=$MY_PASS;?>',id);
socket.on('incMsg', function (msg) {
if(msg.userSend == id) {
msg.text = msg.text.replace(/(\n(\r)?)/g, ' <br/>');
if(msg.userSend == '<?=$MY_LOGIN;?>') msg.img = '<?=$avatar;?>'; else msg.img = '/img/users/<?=$MY_FRIEND_AVATAR;?>';
html = tpl(msg);
$('#messages').append(html);
$("#messages").scrollTop($("#messages")[0].scrollHeight+9999999);
}
});
$('#input').keydown(function(event) {
event = event || window.event;
if(event.keyCode == 13 && !(event.shiftKey) ) {
input = $(this).val();
var newDate = new Date();
if(newDate.getMinutes() < 10) getMinute = '0'+newDate.getMinutes(); else getMinute = newDate.getMinutes();
dateString = newDate.getHours() + ":";
dateString += getMinute + ":";
dateString += newDate.getSeconds();
var msg = {
name: '<?=$MY_LOGIN;?>',
text: input.replace(/(\n(\r)?)/g, ' <br/>'),
time: dateString,
img: '<?=$avatar;?>'
};
html = tpl(msg);
$('#messages').append(html);
$("#messages").scrollTop($("#messages")[0].scrollHeight+9999999);
socket.emit( 'msg', input);
$(this).val('');
event.preventDefault();
}
});
});
});
答案 0 :(得分:0)
问题是你实际上没有连接到mysql。
试试这个:
/* requiring modules */
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'lc',
});
connection.connect(function(err){
//can do something with an error
});
/* your code*/