我想使用php在nodeJs + MYSQL中构建一个聊天系统。这将是一对一的私人聊天,并将在数据库中保存聊天。任何人都知道我需要从哪里开始。Nodejs使用php的私人聊天
目前我得到的服务器验证码:
var app = require('express').createServer()
var io = require('socket.io').listen(app);
app.listen(8181);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
io.sockets.on('connection', function (socket) {
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.emit('updatechat', socket.username, data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
})
你究竟需要什么?你已经知道如何编写一个服务器。你在实现客户端方面有问题吗? – freakish 2012-03-13 07:59:30
感谢feakish参与,我可以建立服务器,作为初学者我很困惑如何使客户端服务器通信实现私人聊天,因为连接正在向所有客户端广播消息。 – 2012-03-13 08:04:31