【问题标题】:How to post multiple object data with socket.io in express如何在 express 中使用 socket.io 发布多个对象数据
【发布时间】:2012-07-31 06:54:13
【问题描述】:

我设法使用 socket.io 发布单个数据,但是如果我添加另一个(时间戳),那么它什么也没有显示。

app.js

app.post('/update', function(req, res, next){
    io.sockets.emit("update", req.body);
    io.sockets.emit("update", Date.now());
    res.send({});
});

add_users.jade

    script(src='jquery-1.7.1.js')
    script(src='http://localhost:3002/socket.io/socket.io.js')
    script(type='text/javascript')
        var socket;
        socket =  io.connect('http://localhost:3002'); 
        socket.on('update', function(newUserAdded, time) {
            console.log("update: ", newUserAdded + "update: ", time)
        });
        $(function() {
            $("button").click(function() {
                $.post("/update", {
                    newUserAdded: $("input").val(),
                    time: getTime()
                });
            });
        });

【问题讨论】:

    标签: javascript jquery node.js express socket.io


    【解决方案1】:

    应该是(new Date).getTime()

    【讨论】:

    • 谢谢,我傻了,哈哈,它奏效了。但它给出了奇怪的数字“1343719031833”,那是什么?
    • 自 1970 年 1 月 1 日以来的毫秒数。如果你想要一些人类可读的东西,你想使用像 toLocaleTimeString() 这样的函数。
    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多