나는 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
감사합니다. 나는 서버를 만들 수 있습니다. 초보자로서 모든 클라이언트에게 메시지를 브로드 캐스팅하기 때문에 클라이언트 서버 통신을 통해 개인 채팅을하는 방법을 혼란스럽게 생각합니다. –