简体   繁体   中英

Sending message from server to client with Java

I'm working with Apache Tomcat 7 ( JSP and Servlets ). In my application, I need to send some messages from server to client. Bellow, I'll explain a little bit what I'm working on.

Brief explanation: The application will bring up a login page if the user isn't logged in every time when he wants to connect to internet. After the user logged in successfully and his time is going to end, I will need to send to client a message with remained time (for example in last few minutes). It can also be another requirement to open advertising popup at a specific time.

I now about JMS but I don't know how fit is that for my scenario. I also read in other posts, the WebSocket can be also an option.

I'm running the server on CentOS 6.2.

Question: For this scenario, do you have some thoughts on how to treat it with Java technologies? If you have some other ideas, feel free to expose!

NB Related to JavaScript and PHP I found good answers on SO's questions. I'm interested on how to solve this issue with Java technologies especially.

http://jwebsocket.org/也许这符合您的需求。

You will not be able to initiate an HTTP connection from the server to the client. One solution will be to use WebSocket/Comet Framework. Unfortunately websockets are not really wide spread (server+browser) for now. I will suggest you to use a framework to fill the "gap": https://github.com/Atmosphere/atmosphere

I don't understand your obsession with us implementing the solution in Java - any valid solution should be portable across different serverside languages. However if the termination is to occur without synchronous user-driven interaction, then you're just creating load on your server by trying to handle it here. If you want somebody to write the code for you then this isn't the right forum.

I now about JMS....CentOS 6.2.

Not much help here.

The thing we really need to know is what you mean by:

After the user logged in successfully and his time is going to end

(I assume you mean the session time is going to end, unless you've written some software which predicts when people will die).

How do you determine when the session will be ended?

  1. Is it a time limit per page?
  2. Is it a fixed time from when they login?
  3. Is it when the session is garbage collected by the Java?

In the case of 1, then the easiest way to achieve this would be to use javascript to set a timeout on the page (when the user navigates to a new screen the timeout will be discarded), eg

setTimeout(function() {
    alert('5 minutes has expired since you got here - about to be logged out');
}, (300000)); // 5 minutes

In the 2nd case then you'd still use the method above, but reduce the timeout on the javascript by the time already spent on the server (write the javascript using java, or drop a cookie containing the javascript timestamp at login).

In the 3rd case.....you don't really have any way of knowing when the user will be logged out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM