将 JSR-356 WebSocket @ServerEndpoint 与 Spring 3 bean 集成 [英] Integrating JSR-356 WebSocket @ServerEndpoint with Spring 3 beans

查看:22
本文介绍了将 JSR-356 WebSocket @ServerEndpoint 与 Spring 3 bean 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring 3.2.5,没有全新的 JSR-356 WebSockets 支持.

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

我希望在我的 @ServerEndpoint WebSocket 服务器中有单例 bean 引用,该服务器由 servlet 容器本身实例化,而不是在 Spring 上下文中.

I would like to have singleton-bean reference in my @ServerEndpoint WebSocket server, which is instantiated by servlet container itself, not in Spring context.

干净的方法是什么?

我目前的解决方案:我用静态字段中的实例制作了 @Service 单例 bean:

My current solution: I made @Service singleton bean with instance in static field:

@Service
public class WebSocketSupportBean {
    private volatile static WebSocketSupportBean instance = null;

    public static WebSocketSupportBean getInstance() {
        return instance;
    }

    public WebSocketSupportBean() {
        instance = this;
    }

并通过静态方法在 @ServerEndpoint 中获取它,如果返回 null 则断开用户连接(如果 bean 在服务器启动期间未创建但用户连接):

and just getting it in @ServerEndpoint by static method, disconnecting user if null returned (if bean not jet created during server startup but user connects):

推荐答案

您可以使用 spring 框架 3.x 设置 websockets

You can setup websockets with spring framework 3.x

我开发了一个小的概念验证应用程序来演示如何,基于 Rossen Stoyanchev 的 SpringConfiguration 与 spring-core 4.0 一起发布.

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

应用程序使用 uri /wstest 设置 websocket 服务器端点,它将使用 @Autowired spring bean 来选择问候语并回复 websocket 消息.

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.

websocket 连接由在支持 websocket 的浏览器中运行的 html 页面 (index.html) 发起并发送消息.

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

端点注册由 ServletContextListener 在上下文初始化时进行,当端点被实例化时,它将与 spring 连接:

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();
        }
    }
}

端点是:

@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<String> {

        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();
            }
        }
    }
}

在我的 Github 页面上查看完整源代码并准备运行示例.

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

这篇关于将 JSR-356 WebSocket @ServerEndpoint 与 Spring 3 bean 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆