long polling netty nio framework java

冷暖自知 提交于 2019-12-03 12:21:29

问题


How can I do long-polling using netty framework? Say for example I fetch http://localhost/waitforx

but waitforx is asynchronous because it has to wait for an event? Say for example it fetches something from a blocking queue(can only fetch when data in queue). When getting item from queue I would like to sent data back to client. Hopefully somebody can give me some tips how to do this.

Many thanks


回答1:


You could write a response header first, and then send the body (content) later from other thread.

void messageReceived(...) {
    HttpResponse res = new DefaultHttpResponse(...);
    res.setHeader(...);
    ...
    channel.write(res);
}

// In a different thread..
ChannelBuffer partialContent = ...;
channel.write(partialContent);



回答2:


You can use netty-socketio project. It's implementation of Socket.IO server with long polling support. On web side you can use Socket.IO client javascript lib.




回答3:


You could also do the following in [sfnrpc]: http://code.google.com/p/sfnrpc

Object object = RPCClient.getInstance().invoke("#URN1","127.0.0.1:6878","echo",true,60,"", objArr,classArr, sl);

The true causes communication to be synchronous.



来源:https://stackoverflow.com/questions/2294010/long-polling-netty-nio-framework-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!