SpringBoot中的WebSocket广播

该例是SpringBoot实战一书中的WebSocket实例,相当于照着打了一遍代码。。。

js下载:

http://download.csdn.net/download/qq_20867981/9959912

添加依赖:

		
			org.springframework.boot
			spring-boot-starter-websocket
			1.3.5.RELEASE
		

配置文件:

package com.example.demo.part3.chapter7.WebSocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
//注解开启使用STOMP协议来传输基于代理message broker的消息
//这时控制器支持使用@messagemapping,就像使用@requestmapping一样
public class Ch763Config extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    注册STOMP协议的节点(endpoint),并映射指定的Url
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        //注册一个stomp协议的endpoint,并指定使用socjJS协议
        stompEndpointRegistry.addEndpoint("/endpointWisely").withSockJS();
    }

    @Override
    //配置消息代理message broker
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //super.configureMessageBroker(registry);
        //广播式应配置一个/topic消息代理
        registry.enableSimpleBroker("/topic");
    }

}


控制层:

package com.example.demo.part3.chapter7.WebSocket;


import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/ws")
public class WsController {
    //这里的MessageMapping类似于RequestMapping
    @MessageMapping("/welcome")
    //当服务端有消息时,会对订阅了@sendto中 多路径的浏览器发送消息
    @SendTo("/topic/getResponse")
    public WiselyResponse sayWelcome(WiselyRequest request){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new WiselyResponse("Welcome"+request.getName()+"!");
    }
}

两个实体类:

package com.example.demo.part3.chapter7.WebSocket;
//浏览器向服务端发送消息用此类接收
public class WiselyRequest {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.example.demo.part3.chapter7.WebSocket;

public class WiselyResponse {
    private String resMsg;

    public WiselyResponse(String resMsg) {
        this.resMsg = resMsg;
    }

    public WiselyResponse() {
    }

    public String getResMsg() {
        return resMsg;
    }

    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }
}

页面:




    
    SpringBoot+WebSocket+广播式




配置viewController,为访问ws.html提供便捷的路径映射:

package com.example.demo.part3.chapter7.WebSocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //super.addViewControllers(registry);
        registry.addViewController("/ws").setViewName("/ws");
    }
}



你可能感兴趣的:(websocket,springboot)