이 문제를 설명하기가 어려워 관련성이 더 높은 용어를 알고 있으면 편집하십시오.실시간 웹 응용 프로그램에서 서버 응답의 컨텍스트 처리
나는 기본적으로 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를 사용하여 응답을 처리 할 필요가있다.
고마워요, 이것은 기본적으로 내가 어떻게 끝내 었는지입니다. – Tom
네, 미안 해요, 제가 대답 할 때까지 이것이 오래된 질문이라는 것을 알지 못했습니다! – jslatts