i used multi room chat application example for node.js writed by mike in this article.and changed it to use session data which grabed from php session handler until now
this is the part of code which i wrote until now
var express = require('express'),
app = express(),
memcache = require("memcache"),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server),
co = require("./cookie.js"),
php = require('phpjs'),
codein = require("node-codein");
//check if user loggedin
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
var cookieManager = new co.cookie(req.headers.cookie);
var client = new memcache.Client(11211, "localhost");
client.connect();
user = client.get("sessions/"+cookieManager.get("sec_session_id"), function(error, result){
var session = JSON.parse(result);
user = JSON.parse(session.name);
user = user.username;
storeUsername(user);
});
});
function storeUsername(user){
// usernames which are currently connected to the chat
var usernames = {};
io.of('/private').authorization(function (handshakeData, callback) {
console.dir(handshakeData);
handshakeData.foo = 'baz';
callback(null, true);
}).io.sockets.on('connection', function (socket) {
usernames[socket.id] = 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 = user;
// add the client's username to the global list
// echo to client they've connected
if(php.in_array(socket.username,usernames)){
delete usernames[socket.username];
}else{
usernames[user] = user;
console.log('not exist');
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');
});
});
}
server.listen(3000);
for example user master will connect to our chatroom and he will have his username which stored from php based application.but where is the problem now?when user master connect from 2 or 3 tab of our browser he will connect to socket server 3 or 4 times and if he post some data we have this result
master : hello
master : hello
master : hello
i want users to connect to my application just once and can post data just once.now how should i achieve that?
how should i access this users in case of private message to each other
i am so new in node.js.my bad.sorry
thanks for help in advance.+1 for all teachers
1) You could you (seems to), var app = require('express').express(); 2) On first app.get, you don't need to put 2 times JSON.parse, maybe the second JSON.parse is not what you want (are you trying to retrieve user threw that field ?) 3) MOST IMPORTANT : to make usage of room, you must use socket.join to join a room, if you don't do it, the socket.broadcast will have no special effect... To remove a user from a room, use socket.leave