我想使用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