当info.password1和info.password2不相同时,我发送一个套接字:
if(info.password1 != info.password2) {
socket.emit('registerError', {value: 'passwordNotTheSame'});
console.log("Register errors");
}
在客户端:
socket.on('registerError', function(error) {
console.log("error!");
});
但是当我发送套接字用于注册而不是相同的密码它不会工作时,它应该显示在控制台日志中'错误!':
完整代码: server.js:
socket.on('requestRegister', function(info) {
if(!info.username || !info.password1 || !info.password2 || !info.email) {
socket.emit("rebuild", {value: true});
socket.emit('startChatInfo', { rooms: rooms, users: users });
return;
}
var checks = ['username', 'password1', 'password2', 'email'];
var check = [];
for(var i = 0; i < checks.length; i++) {
if(checks[i].trim() == "") {
check.push(checks[i]);
}
}
if(check.length != 0) {
for(var index = 0; index < check.length; index++) {
socket.emit('registerError', {value: check[i]});
var indexOf = check.indexOf(check[index]);
if(indexOf > -1) {
check.splice(indexOf, 1);
}
}
} else {
if(info.password1 != info.password2) {
socket.emit('registerError', {value: 'passwordNotTheSame'});
console.log("Register errors");
} else if(!validateEmail(info.email)) {
socket.emit('registerError', {value: 'emailNotValid'});
} else {
mysql.query("SELECT COUNT(username) as founds FROM users WHERE username = "+mysql.escape(info.username)+" LIMIT 1", function(result) {
if(result[0].founds == 0) {
mysql.query("INSERT INTO users(username, password, email, ip_registed, ip_last) VALUES ("+mysql.escape(info.username)+", "+mysql.escape(info.password)+", "+mysql.escape(info.email)+", "+address.address+", "+address.address+" )", function(err) {
socket.emit('requestLogin', {username: info.username, password: info.password});
});
} else {
socket.emit('registerError', {value: 'usernameFull'});
}
});
}
}
});
客户端:
function register() {
var checks = ['#inputUsernamer', '#inputPasswordr1', '#inputPasswordr2', '#inputEmailr'];
var shakes = [];
for(var i = 0; i < checks.length; i++) {
if($( ""+checks[i]+"" ).val().trim() == "") {
shakes.push(checks[i]);
}
}
for(var index = 0; index < shakes.length; index++) {
$( ""+shakes[index]+"" ).effect( "shake" );
var indexOf = shakes.indexOf(shakes[index]);
if(indexOf > -1) {
shakes.splice(indexOf, 1);
}
}
if(shakes.length == 0) {
if($("#inputPasswordr1" ).val() != $( "#inputPasswordr1" ).val()) {
alert("De wachtwoorden komen niet overeen!");
} else if(!validateEmail($( "#inputEmailr" ).val())){
alert("Het email adres is niet geldig!");
} else {
socket.emit("requestRegister", {
username: $( "#inputUsernamer" ).val(),
password1: $( "#inputPasswordr1" ).val(),
password2: $( "#inputPasswordr2" ).val(),
email: $( "#inputEmailr" ).val()
});
}
}
socket.on('registerError', function(error) {
console.log("error!");
});
}