【问题标题】:Nodejs could not send message to online specific soket节点 Js 无法将消息发送到在线特定套接字
【发布时间】:2016-12-13 13:09:36
【问题描述】:

我正在尝试基于 socket.io 实现简单的推送通知,在我的简单代码中练习如何做到这一点我编写了简单的代码,在我的代码中我无法向连接的用户发送消息,当使用记录进入系统我用redis上的密钥存储socket.io,但是在向用户发送消息时,我从redis获取密钥作为用户名并从redis存储socket.io值,我正在尝试发送,我得到相同的@987654324 @value 但消息不发送给用户

客户端:

<!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('message', 'a');
            });
        });
    </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('login', function (message) {
            console.log(message);
        });

        socket.on('message', function (message) {
            console.log(message);
        });

        socket.on('hello,', function (message) {
            console.log(message);
        });

        socket.on('success', function (data) {
            console.log(data);
        });

    </script>
</head>
<body>
<h3 id="login">
    login
</h3>

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

服务器端:

var socket      = require('socket.io'),
    express     = require('express'),
    app         = express(),
    server      = require('http').createServer(app),
    io          = socket.listen(server),
    port        = process.env.PORT || 3000,
    redis       = require("redis"),
    redisClient = redis.createClient();

socket.on('connection', function (socket) {
    socket.on('login', function (data) {
        login(data.username, data.password, function (success, value) {
            if (success) {
                redisClient.exists(data.username, function (err, doesExist) {
                    if (err) return;
                    if (!doesExist) {
                        redisClient.set(data.username, socket.id);
                    }
                    else {
                        redisClient.del(data.username);
                        redisClient.set(data.username, socket.id);
                    }
                    log.info("SOCKET ID: " + socket.id);
                });
                socket.emit('login', {result: success, id: socket.id});
            } else {
                socket.emit('login', {result: false, id: socket.id});
            }
        });
    });
    socket.on('message', function (username) {
        redisClient.get(username, function (err, socketId) {
            if (err)
                socket.emit('message', err);
            io.to(socketid).emit('message', 'for your eyes only');
        });
    });

});

当我点击login 我得到这个结果:

 Object { result=true,  id="/#A5sR-Xo-Owo97NvRAAAB"}

当我点击消息时,我得到这个结果:

user not connected /#A5sR-Xo-Owo97NvRAAAB

socket.ids 都相同,但是客户端没有收到任何消息,服务器返回此消息为user not connected

【问题讨论】:

    标签: node.js sockets socket.io


    【解决方案1】:

    您的代码中似乎有错字:

    redisClient.get(username, function (err, socketId) {
        if (err)
            socket.emit('message', err);
        io.to(socketid).emit('message', 'for your eyes only');
    });
    

    您的回调接收socketId 作为第二个参数,但您在发送消息时使用socketid(小写)。

    另外最好在发送错误消息时退出函数:

    if (err) return socket.emit('message', err);
    

    或:

    if (err) {
        socket.emit('message', err);
        return;
    }
    

    否则您可能会遇到意外错误。

    希望这会有所帮助。

    【讨论】:

    猜你喜欢
    • 2019-01-22
    • 2018-06-05
    • 2016-11-04
    • 2016-05-04
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多