开发者

web-based chat using comet in Java?

开发者 https://www.devze.com 2023-03-29 01:09 出处:网络
I am new to comet. 开发者_如何学JAVA can anyone tell me how to implement a web based chat using comet in java ( Any webtechnologies Eg. JSP ,servlet struts etc. ) ..??

I am new to comet.

开发者_如何学JAVA

can anyone tell me how to implement a web based chat using comet in java ( Any webtechnologies Eg. JSP ,servlet struts etc. ) ..??

Any help is greatly appriciated.

Thanks.


Set up a basic Struts2 application using maven (Struts2 web site).

Add the spring plugin.

Create a service Object which will hold a log of chat messages (List<Map<int, String>>). Each message added adds a new map to the list with an index 1 greater than the previous with the submitted message. If the list reaches a certain size delete the lowest messages to keep the max size. (There are better data structure's for this but you can figure that out).

Add the Struts2-JSON-plugin.

Create two json actions (read how to use the Struts2-JSON-plugin). SendMessage and GetMessagesSince Action. Also create a basic action to create the chat page (I would also use the conventions plugin).

SendMessage will have a setter for message. It would be a good idea to have a getter for Status (returns "success" or "failure").

GetMessagesSince has a getter for Messages which will return a list of {int, Strings}. It will have a setter for lastMessage(int).

Now for the WebPage. At this time simply use short polling (call in a loop after a short break) to get messages greater than 'lastMessage' (which will start at zero). Using jQuery magic you'll get back the response from GetMessagesSince (which will start the chat client off with a little bit of history, if there was any) You will use the last message in the list to derive the new value for GetMessagesSince.

Now with some concurrency magic you can implement long polling. You can also add dates to the messages so people know how old the conversations are. You can also add different message groups, by creating Maps of your chat queue, ie: Map<String, List<Map<int, String>>> where I've chosen string in this case to identify your chat room.

If you're serious, I might even create a demo (It's easier than it sounds).

0

精彩评论

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