1

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

我正在构建一个 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 处理响应。

4

1 回答 1

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 项目

于 2011-04-19T22:53:50.157 回答