2011-02-08 91 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

相關問題