2011-02-08 3 views
1

이 문제를 설명하기가 어려워 관련성이 더 높은 용어를 알고 있으면 편집하십시오.실시간 웹 응용 프로그램에서 서버 응답의 컨텍스트 처리

나는 기본적으로 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를 사용하여 응답을 처리 할 필요가있다.

답변

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

+0

네, 미안 해요, 제가 대답 할 때까지 이것이 오래된 질문이라는 것을 알지 못했습니다! – jslatts