2011-02-08 52 views
1

发现难以描述此问题 - 所以如果您知道更多相关术语,请编辑。处理实时Web应用程序中服务器响应的上下文

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

我有双向沟通工作没有问题 - 但我需要能够从客户端(异步)向服务器发出请求并处理响应,同时仍处理其他可能会出现的无关响应它。

这是我到目前为止,但我不是特别满意这个办法:

服务器

// 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;博士:我需要'询问'服务器,然后使用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

+0

谢谢,这基本上是我最终做到的。 – Tom 2011-04-23 13:07:47

相关问题