Integrating JSR-356 WebSocket @ServerEndpoint with Spring 3 beans

后端 未结 5 1761
夕颜
夕颜 2020-12-18 00:29

I\'m using Spring 3.2.5 without full new JSR-356 WebSockets support.

I would like to have singleton-bean reference in my @ServerEndpoint WebSocket serve

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 01:28

    You can setup websockets with spring framework 3.x

    I developed a small proof-of-concept application to demonstrate how, based on Rossen Stoyanchev's SpringConfiguration released with spring-core 4.0.

    The application sets up a websocket server endpoint with uri /wstest which will use a @Autowired spring bean to select a greeting word and reply to a websocket message.

    The websocket connection is initiated and messages sent by an html page (index.html) running in a browser that supports websockets.

    The Endpoint registration is made by a ServletContextListener at context initialization and when the endpoint is instantiated it will be wired with spring:

    @WebListener
    public class MyApplication implements ServletContextListener {
    
        private final static String SERVER_CONTAINER_ATTRIBUTE = "javax.websocket.server.ServerContainer";
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
    
            ServletContext container = sce.getServletContext();
    
            final ServerContainer serverContainer = (ServerContainer) container.getAttribute(SERVER_CONTAINER_ATTRIBUTE);
            try {
                serverContainer.addEndpoint(new MyEndpointConfig(MyEndpoint.class, "/wstest"));
            } catch (DeploymentException e) {
                e.printStackTrace();
            }
        }
    }
    

    And the Endpoint is:

    @Component
    public class MyEndpoint extends Endpoint {
    
        @Autowired
        MyService myService;
    
        @Override
        public void onOpen(Session session, EndpointConfig config) {
    
            session.addMessageHandler(new MyMessageHandler(session));
        }
    
    
        class MyMessageHandler implements MessageHandler.Whole {
    
            final Session session;
    
            public MyMessageHandler(Session session) {
                this.session = session;
            }
    
            @Override
            public void onMessage(String message) {
                try {
                    String greeting = myService.getGreeting();
                    session.getBasicRemote().sendText(greeting + ", got your message (" + message + "). Thanks ! (session: " + session.getId() + ")");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    Checkout the full source and ready to run example on my Github page.

提交回复
热议问题