【问题标题】:How to run JavaScript code once the connection is established?建立连接后如何运行 JavaScript 代码?
【发布时间】:2015-04-17 19:06:21
【问题描述】:

作为 Node.js 新手,我正在尝试更改 Nick Anastasov 的 Smartphone Remote Control with Node.js and Socket.io 示例,以便不需要密码。

在原始源代码中,app.js 文件在将单词 kittens 输入网络表单和 script.js 后发送 { access : 'granted' } 开始其主要任务,例如取消模糊网页:

socket.on('access', function(data) {
    if (data.access === "granted") {  // XXX Tried removing this line

        blurredElements.removeClass('blurred');
        form.hide();

        $(window).on('hashchange', function() {
            // ....
        });

        socket.on('navigate', function(data) {
            // ....
        });
    }
});

我已更改 app.js,使其根本不会发出 access

然后我尝试从 script.js 中仅删除 if (data.access === "granted") 行,但这当然没有奏效:网页保持模糊,因为不再收到 access 事件(granteddenied 都不是)。

我的问题是:我应该如何更改上面的代码,以便在建立连接后运行?

我应该使用socket.on('connect')socket.on('connection') 还是同样的问题(因为没有发送字符串connect)?

更新:

按照 Brad 的建议(谢谢!)我已将 script.js 中的字符串从“access”更改为“connection”,但网页仍然模糊 - 这是完整的源代码:

$(function() {
    Reveal.initialize({
        history: true
    });

    var socket = io();
    var presentation = $('.reveal');

    socket.on('connection', function(data){ // XXX changed this

                presentation.removeClass('blurred'); // XXX not called
                var ignore = false;

                $(window).on('hashchange', function(){
                        if (ignore){
                                return;
                        }
                        var hash = window.location.hash;
                        socket.emit('slide-changed', {
                                hash: hash
                        });
                });

                socket.on('navigate', function(data){
                        window.location.hash = data.hash;
                        ignore = true;
                        setInterval(function () {
                                ignore = false;
                        },100);
                });
    });
});

这里是完整的 app.js 源代码:

var express = require('express'),
    app = express();

var port = process.env.PORT || 8080;
var io = require('socket.io').listen(app.listen(port));
app.use(express.static(__dirname + '/public'));

var presentation = io.on('connection', function (socket) {
    socket.on('slide-changed', function(data){
                presentation.emit('navigate', {
                        hash: data.hash
                });
    });
});

【问题讨论】:

    标签: node.js express socket.io connection reveal.js


    【解决方案1】:

    是的,你想要io.on('connection')。每当有新客户端连接时都会触发此事件。

    http://socket.io/docs/

    【讨论】:

    • 所以“连接”很特别,不像“访问”?你的意思是io.on('connection')还是socket.on('connection')
    • @AlexanderFarber 新版本的 Socket.IO 文档将对象分配给io,但不管你怎么称呼它。 socket 似乎对你正在做的事情是正确的。 connection 事件是从 Socket.IO 本身内部触发的。这不是远程触发的事件。
    • 我已更改为socket.on('connection'),但网页仍然模糊。您能否看看我更新的问题 - 我在哪里列出了完整的源代码?
    • @AlexanderFarber 哦,connection 事件在服务器上。如果要跟踪该客户端,则需要connect 事件,如下所述:socket.io/docs/client-api
    猜你喜欢
    • 2013-01-30
    • 2017-05-02
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2015-11-01
    • 2018-01-31
    • 1970-01-01
    相关资源
    最近更新 更多