【问题标题】:Atuthentication with socket io in android for chatting app在 android 中使用套接字 io 进行身份验证以用于聊天应用程序
【发布时间】:2016-10-17 11:43:15
【问题描述】:

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

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

/#IyQ7LaKzLClf7g3DAAAA

因此,我无法跟踪特定用户来发送消息。

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

我是 node 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
            });
        }
    });
});

【问题讨论】:

  • 如果连接 ID 动态变化,那么为了与聊天中的特定用户进行通信,您可以在服务器端维护一个注册表。您可以使用数据库或简单的 HashMap 来处理运行时的少量数据
  • @rev_dihazum 感谢您的回复。问。 1 .对于节点js,哪个数据库适合?我的应用程序已经使用 MySQL 进行身份验证。 Qs 2. 离线消息;我会把它存储到数据库吗?
  • 您可以使用现有的数据库。此外,为了只维护一个注册表,您还可以使用任何 NoSQL 数据库,如 mongoDB、couchDB 等,以获得更好的性能。当您的应用处于离线状态时,android 端的离线消息可能无法连接到服务器端 mysql!。您可以将离线消息保存在您的设备上;例如SQLite。

标签: android node.js socket.io


【解决方案1】:

你的socket.on('say to someone', function (id, msg)的实现有问题

请看下面的链接

Sending message to a specific ID in Socket.IO 1.0

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 2020-12-04
    • 1970-01-01
    • 2014-12-19
    • 2011-06-27
    • 2015-01-15
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多