使用XML配置spring-websocket服务

在 使用spring-websocket包搭建websocket服务 一文中我们使用的是注解方式的来配置websocket。

下面我来将其改成xml方式的配置

一。更新Spring的xml命名空间,使其支持WebSocket

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/websocket   
                        http://www.springframework.org/schema/websocket/spring-websocket.xsd">

namespace其中添加了

xmlns:websocket="http://www.springframework.org/schema/websocket"   
http://www.springframework.org/schema/websocket   
http://www.springframework.org/schema/websocket/spring-websocket.xsd" 

二。在context:component-scan 取消对WebSocketConfig的扫描


三。配置websocket:handlers

    <bean id="wsHandler" class="org.jstudioframework.websoket.handler.WebSocketHandler"/>

    <websocket:handlers>
        <websocket:mapping path="/websocket/socketServer.do" handler="wsHandler"/>
        <websocket:handshake-interceptors>
            <bean class="org.jstudioframework.websoket.interceptor.WebSocketHandshakeInterceptor"/>
        </websocket:handshake-interceptors>
    </websocket:handlers>


四,完整配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/websocket
                        http://www.springframework.org/schema/websocket/spring-websocket.xsd">

    <context:annotation-config/>

    <mvc:annotation-driven/>

    <context:component-scan
            base-package="org.jstudioframework.websoket.controller"/>

    <bean id="wsHandler" class="org.jstudioframework.websoket.handler.WebSocketHandler"/>

    <websocket:handlers>
        <websocket:mapping path="/websocket/socketServer.do" handler="wsHandler"/>
        <websocket:handshake-interceptors>
            <bean class="org.jstudioframework.websoket.interceptor.WebSocketHandshakeInterceptor"/>
        </websocket:handshake-interceptors>
    </websocket:handlers>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

五,运行测试,效果一样


Spring Boot Starter Websocket是一个轻量级的启动器,用于快速集成WebSocket支持到你的Spring Boot应用中。在客户端使用它,你可以利用Spring框架提供的WebSocket API,例如`org.springframework.web.socket`包下的类,如`TextMessage`、`BinaryMessage`和`WebSocketSession`。 以下是基本步骤: 1. 添加依赖:在你的`pom.xml`或`build.gradle`文件中添加Spring WebSocket的依赖,对于Maven示例: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 2. 创建WebSocket配置:如果需要自定义WebSocket处理器,可以在Application中创建一个`WebSocketHandler`,并注册它到`WebSocketTransportRegistration`: ```java @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/websocket").withSockJS(); } } ``` 3. 客户端连接:在JavaScript(常常通过Angular或React)中,使用`WebSocket`对象创建连接,比如: ```javascript const socket = new WebSocket('ws://your-app-url/websocket'); ``` 然后可以发送消息、接收事件等操作。 4. 发送和接收消息:客户端可以使用`send()`方法发送消息,监听`message`事件来处理服务器发来的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值