1
我爲我的網站開發即時消息系統,我有兩個問題。首先是console.log不起作用,所以我不能跟蹤腳本執行。第二個是我無法看到傳入的消息,只有我自己輸入的消息,並且消息沒有添加到數據庫中。可能問題在於mysql請求或mysql連接。請幫我解決這些問題node.js + socket.io聊天應用程序
服務器
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();
}
});
});
});
的問題是,我不能從數據庫中獲取用戶名和密碼,查詢不工作。 – user3140144