【问题标题】:Dealing with context of server responses in realtime web applications在实时 Web 应用程序中处理服务器响应的上下文
【发布时间】:2011-06-23 07:44:43
【问题描述】:

很难描述这个问题 - 如果您知道更多相关术语,请进行编辑。

我正在构建一个 Web 应用程序,它基本上使用 Redis (PubSub) + Node.js + Socket.IO 作为分发服务器。

我有双向通信没有问题 - 但我需要能够从客户端(异步)向服务器发出请求并处理响应,同时仍处理可能在它之前出现的其他不相关响应.

这是我目前所拥有的,但我对这种方法并不特别满意:

服务器

// Lots of other code
redis.psubscribe('*');
redis.on("pmessage", function(pattern, channel, message) {
     // broadcast
});

io.on('connection', function(client) {
     client.on('message', function(message) {
         switch(message.method) {
             // call relevant function
         }
     });
 });

 function object_exists(object_id) {
     // do stuff to check object exists
     client.send({method: 'object_exists', value: object_exists});
 }

客户

var call = Array();
$(document).ready(function() {
    socket.connect();
    socket.on("message", function(obj){
        console.log(obj);
        call[obj.method](obj.value);
    });
});

function object_exists(object_id) {
    socket.send({method: 'object_exists', value: object_id});
    // Set a function to be called when the next server message with the 'object_exists' method is received.
    call['object_exists'] = function(value) {
        if(value) {
            // object does exist
        }
    }
}

tl;dr:我需要向服务器“询问”一些事情,然后使用 Socket.IO 处理响应。

【问题讨论】:

    标签: asynchronous node.js real-time redis socket.io


    【解决方案1】:

    您没有具体说明您对自己的方法不满意的原因,但在我看来,您就快到了。我不太确定你想用调用数组做什么,所以为了清楚起见,我把它拿出来了。

    基本上,您只需要设置一个 switch 语句来充当套接字连接每一端的消息路由器,并根据传入消息触发适当的方法。使用消息本身发送足够的状态,这样您就可以在没有任何额外上下文的情况下处理工作。在您重新编写的代码中,我将 object_id 发送到服务器并再次发送回客户端。

    ///SERVER
    // Lots of other code
    redis.psubscribe('*');
    redis.on("pmessage", function(pattern, channel, message) {
         // broadcast
    });
    
    io.on('connection', function(client) {
         client.on('message', function(message) {
             switch(message.method) {
                case 'object_exists':
                    object_exists(message.objectId);
                break;
             }
         });
     });
    
     //Takes an id an returns true if the object exists
     function object_exists(object_id) {
         // do stuff to check object exists
         client.send({method: 'object_exists', objectId: object_id, value: object_exists});
     }
    
    ///CLIENT
    $(document).ready(function() {
    
        //setup the message event handler for any messages coming back from the server
        //This won't fire right away
        socket.on("message", function(message){
             switch(message.method) {
                case 'object_exists':
                    object_exists(message.objectId, message.value);
                break;
             }
        });
    
        //When we connect, send the server the message asking if object_exists
        socket.on("connect", function() {
            socket.send({method: 'object_exists', objectId: object_id});
        });
    
        //Initiate the connection
        socket.connect();
    });
    
    //Get's called with with objectId and a true if it exists, false if it does not
    function object_exists(objectId, value) {
            if(value) {
                // object does exist, do something with objectId
            }
            else {
                // object does not exist
            }
        }
    

    如果您想在同一个堆栈中看到更多代码执行与您尝试完成的工作类似的工作,请查看我的nodechat.js project

    【讨论】:

    • 谢谢,这基本上是我最终的做法。
    • 是的,很抱歉,在我回答之前我才发现这是一个老问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多