从Android设备向其他设备发送消息给特定用户

时间:2016-08-08 16:49:18

标签: android node.js sockets socket.io

我实现的简单聊天服务器在各种浏览器中运行良好,但是无法从特定用户向其他Android设备或Android设备发送消息给从浏览器连接的用户,服务器说我用户没有连接,但在浏览器中可以正确检测并发送消息,我不知道什么是问题,我在android上使用nkzawa/socket.io-android-chat库并且所有其他功能都正常工作< / p>

用于测试和向特定用户发送消息的Html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>
        Document
    </title>
    </meta>
    <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#login').click(function () {
                socket.emit('login', {username: 'a', password: 'a'});
            });

            $('#mesage').click(function () {
                socket.emit('requestMoney', 'mahdi');
            });
        });
    </script>
    <script>
        var socket = new io.connect('http://192.168.1.35:3000', {
            port      : 3000,
            transports: ['websocket']
        });
        socket.on('connect', function () {
            console.log('connected!');
        });
        socket.on('message', function (message) {
            console.log("hello ::::::::" + message);
        });
        socket.on('success', function (data) {
            console.log(data);
        });
    </script>
</head>
<body>
<h3 id="login">login</h3>

<h3 id="mesage">mesage</h3>
</body>
</html>

我简化的nodejs服务器:

var socket      = require('socket.io'),
    express     = require('express'),
    app         = express(),
    server      = require('http').createServer(app),
    io          = socket.listen(server),
    port        = process.env.PORT || 3000,
    mysql       = require('mysql'),
    multer      = require('multer'),
    uuid        = require('node-uuid'),
    datetime    = require('node-datetime'),
    moment      = require('moment'),
    bcrypt      = require('bcrypt'),
    async       = require('async'),
    request     = require('request'),
    redis       = require("redis"),
    redisClient = redis.createClient(),
    log         = require('log4node');

var io_redis    = require('socket.io-redis');

io.adapter(io_redis({host: 'localhost', port: 6379}));

require('sticky-socket-cluster/replace-console')();

require('sticky-socket-cluster')(options, start);

function start(port) {
    io.sockets.on('connection', function (socket) {
        socket.on('login', function (data) {
            console.log(data.username);
            login(data.username, data.password, function (success, value) {
                if (success) {
                    redisClient.exists(data.username, function (err, doesExist) {
                        if (err) return;
                        if (!doesExist) {
                            log.info("saved to redis");

                            redisClient.set(data.username, socket.id, function (err, res) {
                                redisClient.set(data.username, socket.id);
                            });
                        }
                        else {
                            log.info("deleted and saved to redis");

                            redisClient.del(data.username);
                            redisClient.set(data.username, socket.id, function (err, res) {
                                redisClient.set(data.username, socket.id);
                            });
                        }
                    });
                    socket.emit('login', {result: true, id: value});
                } else {
                    log.info("FAIL login");
                    socket.emit('login', {result: false});
                }
            });
        });

        socket.on('requestMoney', function (data) {
            redisClient.get('mahdi', function (err, socketId) {
                if (io.sockets.connected[socketId]) {
                    log.info('mahdi' + ' sent');
                    io.sockets.connected[socketId].emit('message', {username: 'Hey to :), inja ro bebin ;)'});
                } else {
                    log.info('mahdi' + ' NOT LOGin');
                }
            });
        });

    });

    server.listen(port, function () {
        console.log('Express and socket.io listening on port ' + port);
    });
}

...

Android套接字设置和选项,我使用此库的命名空间:

public class Application extends android.app.Application {
    ...
    public static Socket    CHAT_SOCKET;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        ...
        IO.Options opts = new IO.Options();
        opts.forceNew = true;
        opts.reconnection = true;

        try {
            CHAT_SOCKET = IO.socket(ClientSettings.getChatAddress(), opts);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            Log.e("SOCKET.IO ", e.getMessage());
        }
    }
    public static Context getContext() {
        return context;
    }
    public Socket getSocket() {
        return CHAT_SOCKET;
    }
}

0 个答案:

没有答案