Websocket message push implementation and websocket message push

Source: Internet
Author: User

Websocket message push implementation and websocket message push

I. Service Layer

  

Package com. demo. websocket; import java. io. IOException; import java. util. iterator; import java. util. concurrent. concurrent1_queue; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; import org. springframework. context. annotation. configuration; import org. springframework. web. socket. closeStatus; import org. springframework. web. socket. textMessage; import org. springframework. web. socket. webSocketSession; import org. springframework. web. socket. config. annotation. enableWebSocket; import org. springframework. web. socket. config. annotation. webSocketConfigurer; import org. springframework. web. socket. config. annotation. webSocketHandlerRegistry; import org. springframework. web. socket. handler. listener; @ Configuration @ EnableWebSocketpublic class websocketListener implements WebSocketConfigurer, ServletContextListener {private concurrent1_queue <WebSocketSession> sessions = new concurrent1_queue <WebSocketSession> (); private login handler; @ Override public void contextDestroyed (ServletContextEvent arg0) {// TODO Auto-generated method stub} @ Override public void contextInitialized (ServletContextEvent arg0) {// TODO Auto-generated method stub} @ Override public void registerWebSocketHandlers (WebSocketHandlerRegistry registry) {handler = new WebSocketHandlerTest (); registry. addHandler (handler, "/ws/notifymessage. ws "); registry. addHandler (handler, "/ws/sockjs/yymessage. ws "). withSockJS (); new Thread (handler ). start ();} class WebSocketHandlerTest extends TextWebSocketHandler implements Runnable {@ Override public void afterConnectionClosed (WebSocketSession session, CloseStatus status) throws Exception {sessions. remove (session) ;}@ Override public void afterConnectionEstablished (WebSocketSession session) throws Exception {sessions. add (session) ;}@ Override protected void handleTextMessage (WebSocketSession session, TextMessage message) throws Exception {super. handleTextMessage (session, message) ;}@ Override public void run () {System. out. println ("waiting for push .... "); try {int I = 0; for (;) {synchronized (this) {try {Thread. sleep (3000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}if (I % 10 = 0) {nofity ("message push test ...... "); System. out. println ("message pushed .... ");} else {System. out. println ("no message pushed this time .... ");} I ++;} catch (IOException e) {e. printStackTrace (); System. out. println ("failed .... ") ;}} private void nofity (String message) throws IOException {Iterator <WebSocketSession> iterator = sessions. iterator (); while (iterator. hasNext () {WebSocketSession session = iterator. next (); synchronized (session) {if (session. isOpen () {session. sendMessage (new TextMessage (message);} else {iterator. remove ();}}}}}}

2. Front-End Interface listening

  

<! DOCTYPE html> 

Note that the DispatcherServlet controller is configured in web. xml.

Spring-servlet.xml empty file
<servlet>        <servlet-name>spring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/spring-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>*.ws</url-pattern>    </servlet-mapping>

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.