开发者

Multiple long polling?

开发者 https://www.devze.com 2023-01-29 06:35 出处:网络
I\'m developing a chat application and I\'ve 开发者_如何学运维come across the following thought.. Should I use \'multiple\' long polling requests to my server, each one handling different things. For

I'm developing a chat application and I've 开发者_如何学运维come across the following thought.. Should I use 'multiple' long polling requests to my server, each one handling different things. For example one checking for messages, one for 'is typing', one for managing the contacts list 'is online/offline' etc.. or would it be better to handle it all through one channel?


My opinion is that you’d be better off with one connection, and sending JSON messages back and forth, e.g.:

User joined:

{"user_add": "st3"}

User left:

{"user_left": "sneeu"}

Message received

{"message": "Good morning!", "from": "st3"}

And these could be sent together in an array, so that users could receive everything since their last response.


Polling is going to be your biggest bandwidth/resource hog, so keep it to a minimum; e.g. issue HEAD requests with appropiate date / if-modified-since headers to allow caching to work sensibly, with the server returning just headers containing the date and time of the last change to any of the properties you're interested in - or perhaps something even more minimal than that; and only issue a full GET if the returned headers imply there is new information.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号