Need to use WebSocket in an old project.
Made a small example of websocket, no problem.
When this example is integrated into the STRUTS2 project, the WebSocket is not accessible. It is because the STRUTS2 filter has intercepted all requests, causing Websock to be unable to process requests to/websocket. Workaround: Use the Excludepattern property in the Struts configuration file to filter out the unhandled URLs.
WebSocket need Javaee-api-7.0.jar. STRUTS2 jar packages are not listed here, you can import them yourself.
Print the client message for testing purposes
System.out.println ("Received:" + message);
Send the first message to the client
Session.getbasicremote (). SendText ("This is the first server message");
Send 3 messages to the client every 5 seconds
int sentmessages = 0;
while (Sentmessages < 3) {
Thread.Sleep (5000);
Session.getbasicremote (). SendText ("This was an intermediate server message. Count: "+ sentmessages);
sentmessages++;
}
Send a final message to the client
Session.getbasicremote (). SendText ("The Last Server Message");
}
@OnOpen
public void OnOpen () {
System.out.println ("Client connected");
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.