节点 Js 无法将消息发送到在线特定套接字

Posted

技术标签:

【中文标题】节点 Js 无法将消息发送到在线特定套接字【英文标题】:Nodejs could not send message to online specific soket 【发布时间】: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

【问题讨论】:

【参考方案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;

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

希望这会有所帮助。

【讨论】:

嗨奥列格,我知道你回答这个问题已经有一段时间了,但如果你能看一下,我有一个非常相似的问题。 ***.com/questions/65277256/…

以上是关于节点 Js 无法将消息发送到在线特定套接字的主要内容,如果未能解决你的问题,请参考以下文章

使用 socket.io 向特定客户端发送消息,同时套接字 id 快速变化

套接字 io,节点 js,将图像/文件从服务器发送到客户端的简单示例

如何将数据从 Android 中的数据报套接字发送到 Node js 服务器?

Spring引导Web套接字,无需向特定用户发送消息

使用套接字将数据从 node.js 发送到 Java

在Socket.IO 1.0中将消息发送到特定ID