是否有任何工作示例存储会话并将所有会话用于所有打开的窗口,让一个用户只需连接一个房间一次?
这个应用程序将获得phpsessions作为node.js会话,但我不知道如何只让一个人访问此聊天应用程序
//define what we need
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");
// answer all request from users and send them back index.html for root access
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
var cookieManager = new co.cookie(req.headers.cookie);
//using memcache as our session store
var client = new memcache.Client(11211, "localhost");
//connect to memcache client
client.connect();
//get our cookie sessions
user = client.get("sessions/"+cookieManager.get("sec_session_id"), function(error, result){
var session = JSON.parse(result);
//get just username from sessions(sessions store name and family and chat username in this case)
user = JSON.parse(session.name);
user = user.username;
//use this function to pass our chat username to our function
storeUsername(user);
});
});
function storeUsername(user){
// usernames which are currently connected to the chat
var usernames = {};
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);
一切正常,发送数据的用户已定义,但当我在另一个标签中访问此网站时,我将另一次连接到socket.io服务器
答案 0 :(得分:1)
您的意思是在浏览器标签之间共享websocket吗?
Sharing websocket across browser tabs?
但为什么你需要共享套接字?我为论坛开发了一个node.js聊天,我们不关心用户有多少个套接字。我们只有一个“用户”对象,其中包含一个套接字列表。我们不关心套接字来自firefox,来自Android应用程序......这不是问题。当我们需要向用户发送信息时,我们会将其发送到每个套接字。
答案 1 :(得分:1)
你可以试试这个。它使用express和socket.io https://github.com/gravityonmars/Balloons.IO