I am currently working on a socket.io-client/socket.io login system, the problem is when I start the server, a query is performed, retrieving the current data in the database, if a user registers they are not able to login until the server is restarted, if I use:
我目前正在开发一个socket.io-client / socket.io登录系统,问题是当我启动服务器时,执行查询,检索数据库中的当前数据,如果用户注册他们无法登录直到服务器重新启动,如果我使用:
SELECT * FROM mytable ORDER BY id DESC LIMIT 1
Then only the user that just registered can login, and not users that are already registered, how can I resolve this issue?
那么只有刚注册的用户才能登录,而不是已经注册的用户,我该如何解决这个问题呢?
I have it so when the username and passwords are submitted from the client to the server, the function containing the Mysql code is performed, logically I thought this would run the query again for each new connection, but it doesn't.
我有它所以当用户名和密码从客户端提交到服务器时,执行包含Mysql代码的函数,逻辑上我认为这将为每个新连接再次运行查询,但事实并非如此。
My Code:
Server:
console.log("Berdio - A Awesome socket server for everyone!")
console.log("Created with Love, by OzzieDev! Enjoy the awesomeness!")
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
function dosql(){
var mysql = require('mysql')
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'Berd'
})
connection.query('SELECT * FROM Berd_Data', function(err, rows, fields, result){
if (err) throw err;
var usrname = rows[0].Username
var psword = rows[0].Password
global.usrname = usrname
global.psword = psword
});
}
io.on('connection', function(socket){
})
socket.on('user-name', function(data){
dosql()
socket.broadcast.emit('user-connected',data.u);
console.log(u,p)
var u = data.u
var p = data.p
console.log(u,p + " " + "emitted Data");
if (u === global.usrname && p === global.psword){
socket.emit('AuthGood')
}else{
socket.emit('AuthFail')
}
socket.on('msg',function(data){
var message = data.message
if (typeof message !== "undefined"){
socket.broadcast('newmsg',msg)
}
if(message === "quit"){
process.exit();
}
})
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Client:
var socket = require('socket.io-client')('http://127.0.0.1:3000');
socket.on('connect_error', function(){
console.log('Failed to establish a connection to the servers, or lost connection');
return process.exit();
});
var prompt = require("prompt-sync")()
console.log("Your Ip has been logged, but encrypted. Please see website for more info")
var news = "Add news: Will be from database. "
var username = prompt("Username>: ")
var password = prompt("Password>: ")
if (typeof username, password !== "undefined"){
socket.emit('request')
socket.emit('user-name', { u: username, p: password });
}
socket.on('AuthGood',function(socket){
console.log("Your details have been authenticated, hello again!")
var message = prompt("message>: ")
})
socket.on('AuthFail',function(socket){
console.log("Your details failed to authenticate, Wrong Pass/Username combination, quitting!")
return process.exit();
})
if (typeof message !== "undefined"){
socket.emit('msg',{m: message})
}
socket.on('user-connected',function(data){
var userconn = data.username
console.log(username +" " +"Has joined us!")
})
socket.on('newmsg',function(data){
var newmsg = data.msg
console.log("-----------------------------------")
console.log(username+">:" + " " +msg)
})
1 个解决方案
#1
0
Using MySql query:
使用MySql查询:
SELECT * FROM Berd_Data WHERE `Username` = ?', [u]
Fixed the issue.
修复了这个问题。
#1
0
Using MySql query:
使用MySql查询:
SELECT * FROM Berd_Data WHERE `Username` = ?', [u]
Fixed the issue.
修复了这个问题。