11

我编写了一些代码示例,通过 socket.io 识别连接的用户......所以现在我必须在索引页面上编写代码来与用户通信。

代码如下,如何向用户[1]“欢迎”和用户[2]“HI men”发送消息,并限制 2 个用户的连接。所以当 2 个用户连接时,其他任何人都无法连接..

索引.html:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect();
  var users;
  socket.on('hello', function (data) {
    console.log(data.hello);
  });
  socket.on('listing', function (data) {
     users = data; 
  });
  socket.on('chat', function (message) {
     console.log(message); 
  });
  socket.on('message', function (message) {
     console.log(message); 
  });
  function chat (message) {
    socket.emit('chat', message);   
  }
  function message (user, message) {
    socket.emit('message', {
       user: user,
       message: message
    });
  }
</script>

应用程序.js

var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app);

app.listen(3000);

app.use(express.static(__dirname));

var users = {};
var userNumber = 1;

function getUsers () {
   var userNames = [];
   for(var name in users) {
     if(users[name]) {
       userNames.push(name);  
     }
   }
   return userNames;
}

io.sockets.on('connection', function (socket) {
  var myNumber = userNumber++;
  var myName = 'user#' + myNumber;
  users[myName] = socket;

  socket.emit('hello', { hello: myName });
  io.sockets.emit('listing', getUsers());

  socket.on('chat', function (message) {
    io.sockets.emit('chat', myName + ': ' + message);
  });
  socket.on('message', function (data) {
     users[data.user].emit('message', myName + '-> ' + data.message); 
  });

  socket.on('disconnect', function () {
    users[myName] = null;
    io.sockets.emit('listing', getUsers());
  });
});

app.listen(process.env.PORT);
4

2 回答 2

9

您可以先看看如何使用 Socket.io 配置授权。回调提供的handshakeData可以在那里修改(即:添加用户名属性),并且任何更改都可以通过socket.handshake您的 app.js 访问(通过传递给回调的对象io.sockets.on('connection',..)。使用也可以从 handshakeData 访问的请求标头信息,您可以在授权回调中设置用户值(即:来自数据库),以便您可以在 app.js 中识别给定套接字的用户。

这是一个类似的例子

于 2012-05-21T15:00:22.437 回答
5

我知道你问这个问题已经很久了,但就在 4 天前,我发布了一个用于 node js、express 和 socket.io 的模块,它可以管理你想要的东西。检查用法和示例;我希望你会发现这个模块有帮助!

你可以通过 NPM 安装socket.io.users 这是一个用于 socket.io 应用程序的节点 js 模块。每个客户一个用户。

部分使用代码:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var socketUsers = require('socket.io.users');

// ...
socketUsers.Session(app); // IMPORTANT !
// ...

var rootIo = require('socket.io')(server); // default '/' as namespace. 
var chatIo = rootIo.of('/chat');

var rootUsers = socketUsers.Users; /* default '/' as namespace.
Each namespace has ITS OWN users object list,
but the Id of a user of any other namespace may 
have the same value if request comes from the same client-machine-user.
This makes easy to keep a kind of 
synchronization between all users of all the different namespaces. */

var chatUsers = socketUsers.Users.of('/chat'); //  

rootIo.use(socketUsers.Middleware());
/* IMPORTANT but no errors if you want
to skip it for a io.of(namespace) 
that you don't want the socket.io.users' support. */

chatUsers.use(socketUsers.Middleware());

chatUsers.on('connected',function(user){
console.log(user.id + ' has connected to the CHAT');
user.store.username = 'username setted by server side'; /*at the store
property you can store any type of properties
and objects you want to share between your user's sockets.  */
user.socket.on('any event', function(data){ 
/*user.socket is the current socket, to get all connected sockets from this  
user, use: user.sockets */

});
chatIo.emit('set username',user.store.username);
});

rootUsers.on('connected',function(user){
   console.log('User has connected with ID: '+ user.id);
});



rootUsers.on('connection',function(user){
   console.log('Socket ID: '+user.socket.id+' is user with ID: '+user.id);
});

rootUsers.on('disconnected',function(user){
    console.log('User with ID: '+user.id+'is gone away :(');
});

//...server.listen blabla..
于 2015-06-20T08:39:19.553 回答