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>