我是node.js的新手,使用socket.id来识别登录用户时遇到问题。在客户端使用电子邮件ID和密码进行用户登录,该密码是从服务器端的表中验证的,如果经过验证,用户socket.id用于向该客户端发送用户名。
当两个用户登录时;他们可以使用唯一的用户名聊天,但是当刷新聊天页面时,用户名都会更改为第二次登录的用户名(即稍后登录的用户)。请帮忙。
这是我的代码 server.js
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, Twit = require('twit')
, io = require('socket.io').listen(server)
, os = require('os')
, open = require('open')
, bodyParser = require('body-parser')
, connect = require('connect')
, mysql = require("mysql");
var clients = {};
server.listen(9383,'192.168.1.3');
var name1;
app.get('/loginstyle.css', function (req, res) {
res.sendfile(__dirname + '/loginstyle.css');
});
app.get('/loginme', function (req, res) {
res.sendfile(__dirname + '/loginmysql.html');
});
app.get('/option', function (req, res) {
res.sendfile(__dirname + '/option.html');
});
app.get('/chat', function (req, res) {
res.sendfile(__dirname + '/chat.html');
});
app.post('/valid', function (req, res) {
var username=req.body.email;
var password=req.body.pass;
var connection = mysql.createConnection({
"hostname": "localhost",
"user": "root",
"password": "vk123",
"database": "login"
});
connection.connect();
connection.query('SELECT * FROM id WHERE email=? AND password=?', [username,password], function(err, rows){
if (err){
throw err;
}else{
for (var i in rows) {
name1=rows[i].name;
io.sockets.on('connection', function(socket){
clients[socket.id] = socket;
clients[socket.id].emit('name', name1); // username is sent to client
});
res.redirect('http://192.168.1.3:9383/chat');
}
}
});
connection.end();
});
io.sockets.on('connection', function(socket){
socket.on('chat message', function(name,msg){
console.log(msg);
io.emit('chatmessage',name,msg);
});
});
client.html
<!DOCTYPE html>
<html>
<head>
</head>
<body background="/bgimage.jpg">
<div class="chat"></div>
<form style="bottom:10px; position:fixed; padding-left: 100px">
<input type="text" class="msg" style="width:1000px; height:30px"></input>
<button type="submit" class="enter" style="padding-left:0px; height:35px; background-color:skyblue">Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
var socket = io.connect('http://192.168.1.3:9383');
var username;
var chatmsg=[];
socket.on('name', function(name){
username=name;
console.log(username);
});
$('.enter').bind("click",function(){
chatmsg.push({chatname:username, msgtext:($('.msg').val())});
socket.emit('chat message',username,$('.msg').val() );
$('.msg').val('');
return false;
});
socket.on('chatmessage', function(name,msg){
var block= $("<div class='message'> </div>");
if(msg.length){
var messenger= $("<div style='color:green;font-weight:bold;background-color:floralwhite'> </div>");
messenger.append(name);
block.append(messenger);
var text=$("<div style='padding:3px;background-color:floralwhite'></div>");
text.append(msg);
block.append(text);
block.append($("<div style='padding:10px;background-color:transparent'></div>"));
$('.chat').append(block);
}
});
});
</script>
</body>
</html>