在android中用于聊天应用程序的套接字io的身份验证

时间:2016-06-16 06:00:36

标签: android node.js socket.io

我正在使用android中的socket.io开发一对一聊天应用程序。我可以发送&从单个聊天室接收消息。我关注this tutorial。我的应用聊天模块看起来像that。现在,我想发送&从单个用户接收消息。

在开发过程中,我观察到每个套接字连接,socket.io都为客户端提供了一个新的ID,如下所示:

/#IyQ7LaKzLClf7g3DAAAA

由于这个原因,我无法跟踪发送消息的特定用户。

问题1.我是否必须连接任何数据库以存储用户凭据,以便向特定用户发送消息&还要向他/她发送离线消息?聊天功能是我的Android应用程序中的附加功能。目前,此应用使用基于令牌的身份验

我是节点js&的新手socket.io。给我一个建筑指南如何解决这个问题。 TIA。

我的app.js代码:

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


var port = process.env.PORT || 3000;

server.listen(port, function () {
    console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(__dirname + '/public'));

// Chatroom
var numUsers = 0;
var clients = [];

io.on('connection', function (socket) {
    var addedUser = false;
    clients.push(socket);

    console.log('one user connected: user name: ' +socket.username +"------ id : >> "+ socket.id);
    console.log('Total User List:' + clients);

    socket.on('connect', function (data) {
        // we tell the client to execute 'new message'
        socket.broadcast.emit('connect', {
            username: socket.username,
            numUsers: numUsers,
            socket_id:socket.id
        });
    });

    // when the client emits 'new message', this listens and executes
    socket.on('new message', function (data) {
        // we tell the client to execute 'new message'
        socket.broadcast.emit('new message', {
            username: socket.username,
            message: data
        });
    });

    // when the client emits 'add user', this listens and executes
    socket.on('add user', function (username) {
        if (addedUser) return;

        // we store the username in the socket session for this client
        socket.username = username;
        ++numUsers;
        addedUser = true;
        socket.emit('login', {
            numUsers: numUsers
        });
        // echo globally (all clients) that a person has connected
        socket.broadcast.emit('user joined', {
            username: socket.username,
            numUsers: numUsers,
            socket_id:socket.id
        });
    });

    // when the client emits 'typing', we broadcast it to others
    socket.on('typing', function () {
        socket.broadcast.emit('typing', {
            username: socket.username
        });
    });

    // when the client emits 'stop typing', we broadcast it to others
    socket.on('stop typing', function () {
        socket.broadcast.emit('stop typing', {
            username: socket.username
        });
    });

    // when want to send message to specific user
    socket.on('say to someone', function (id, msg) {

        socket.broadcast.to(id).emit('say to someone', {
            username: socket.username,
            id:id,
            message: msg
        });

    });

    // when the user disconnects.. perform this
    socket.on('disconnect', function () {

        clients.splice(clients.indexOf(socket), 1);
        console.log('Disconnected... ' + socket.id);
        if (addedUser) {
            --numUsers;
            // echo globally that this client has left
            socket.broadcast.emit('user left', {
                username: socket.username,
                numUsers: numUsers
            });
        }
    });
});

1 个答案:

答案 0 :(得分:0)

您的public class SystemAccessList { public SystemAccess SystemAccess { get; set; } public List<SystemAccess> SystemAccessList { get; set; } SystemDbContext db; public void setDbContext(PALMSConfigDbContext _db) { db = _db; } public SystemAccessList GetAccessList(SystemAccessList systemAccessList) { var qresult = db.tbl_SystemAccessList .GroupBy(g => g.ClassID) .AsEnumerable().Select(c => { var rr = new ResultRow { Class = c.Key }; foreach (var r in db.tbl_SystemAccessList.GroupBy(gg => gg.StudentID)) { rr.Student[r.Key] = "N"; } foreach (var r in c.Where(w => w.ClassID == c.Key)) { rr.Student[r.StudentID] = "Y"; } return rr; }).ToList(); systemAccessList.SystemAccessList.AddRange(qresult); return systemAccessList; } class ResultRow { public ResultRow() { Student = new Dictionary<string, string>(); } public string Class { get; set; } public IDictionary<string, string> Student { get; set; } public string GetHeader() { return Student.Aggregate("Class", (current, s) => current + "|" + s.Key); } public string GetSolidRow() { return Student.Aggregate(Class, (current, s) => current + "|" + s.Value); } } public class SystemAccess { [Key] public int ID { get; set; } public string ClassID { get; set; } public string StudentID { get; set; } }

的实施存在问题

请参阅以下链接

Sending message to a specific ID in Socket.IO 1.0

https://auth0.com/blog/2014/01/15/auth-with-socket-io/

相关问题