WebSocket

  1. WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。
  2. 在WebSocket API中,浏览器和服务器只需要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道。两者之间就直接可以数据互相传送。
    浏览器通过 JavaScript 向服务器发出建立 WebSocket 连接的请求,连接建立以后,客户端和服务器端就可以通过 TCP 连接直接交换数据。
    当你获取 Web Socket 连接后,你可以通过 send() 方法来向服务器发送数据,并通过 onmessage 事件来接收服务器返回的数据

  3. spring整合websocket
  4. 添加maven依赖
                <!-- spring websocket 开始-->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-messaging</artifactId>
    			<version>4.0.6.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-websocket</artifactId>
    			<version>4.0.6.RELEASE</version>
    		</dependency>
    				<dependency>
    		    <groupId>javax.websocket</groupId>
    		    <artifactId>javax.websocket-api</artifactId>
    		    <version>1.0</version>
    		    <scope>provided</scope>
    		</dependency>
    		<!-- spring websocket 结束-->    
  5. 配置task注解applicationContext-task.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:task="http://www.springframework.org/schema/task"  
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
            >
    <!--     开启这个配置,spring才能识别@Scheduled注解    -->   
             <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
        
             <task:scheduler id="qbScheduler" pool-size="10"/>  
        
        
        
    </beans>

     

  6. 通过websocket定时刷新需要交互的数据
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import org.springframework.web.socket.TextMessage;
    import org.springframework.web.socket.WebSocketSession;
    
    import com.alibaba.fastjson.JSON;
    import com.founder.szhd.business.water.dto.WaterAlarmInfoDTO;
    import com.founder.szhd.business.water.service.WaterAlarmInfoService;
    
    /**
     * 通过webSocket定时刷新前端数量
     *
     */
    @Component
    public class FrontWebDataSchedule {
    	
    	private final static Logger logger = LoggerFactory.getLogger(FrontWebDataSchedule.class);
    	
    	@Autowired
    	private WaterAlarmInfoService<WaterAlarmInfoDTO> waterAlarmInfoService;
    	
    	 @Bean//这个注解会从Spring容器拿出Bean
        public CustomWebSocketHandler infoHandler() {
     
            return new CustomWebSocketHandler();
        }
    	 
    	 /**
    	     * @return void
    	     * @throws
    	     * @Description: TODO   定时更新报警信息
    	     * @author xiehui
    	     * @date 2018/8/25 上午11:58
    	     */
    	    @Scheduled(fixedDelayString = "1500")
    	    protected void updateWarnInfo() throws IOException {
    	    	 Map<String, WebSocketSession> users = infoHandler().getUsers();
    	        System.out.println(" 定时更新报警信息");
    	        
    	        Set<String> mchNos = users.keySet();
    	        WebSocketSession session = null;
    	        for (String mchNo : mchNos) {
    	            session = users.get(mchNo);
    				String departId = String.valueOf(session.getAttributes().get("departId"));
    				Map warnInfoMap = new HashMap();
    				warnInfoMap.put("navi", getNaviWarnInfo(departId));
    				String message = JSON.toJSONString(warnInfoMap);
    				infoHandler().sendMessageByDepartment(new TextMessage(message), departId);
    	        }
    	    }
    	    
    	    /**
    	     * 查询报警信息
    	     * @param key
    	     * @return
    	     */
    	    private Map<String, Object> getNaviWarnInfo(String key) {
    	        Map<String, Object> waterMap = null;
    	        try {
    	            waterMap = waterAlarmInfoService.findWarningPro(key);
    	        } catch (Exception e) {
    	            logger.error(e.getMessage());
    	        }
    	        return waterMap;
    	    }
    	    
    	 
    
    } 
  7. springboot对websocket支持很友好,只需要继承webSocketHandler类,重写几个方法就可以了,这个类的作用就是在连接成功前和成功后增加一些额外的功能
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    import org.apache.commons.lang.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.socket.CloseStatus;
    import org.springframework.web.socket.TextMessage;
    import org.springframework.web.socket.WebSocketHandler;
    import org.springframework.web.socket.WebSocketSession;
    import org.springframework.web.socket.handler.TextWebSocketHandler;
    
    /**
     * 创建一个WebSocket server
     * 
     *
     */
    @Service
    public class CustomWebSocketHandler extends TextWebSocketHandler implements WebSocketHandler {
        private Logger logger = LoggerFactory.getLogger(CustomWebSocketHandler.class);
        // 在线用户列表
        private static final Map<String, WebSocketSession> users;
        
     // 用户标识
        private static final String CLIENT_ID = "username";
        
        @Autowired
        private FrontWebDataSchedule frontWebData;
    
    
        static {
            users = new HashMap<>();
        }
        
    
        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        	logger.info("成功建立websocket-spring连接");
            String mchNo = getMchNo(session);
            if (StringUtils.isNotEmpty(mchNo)) {
                users.put(mchNo, session);
               // session.sendMessage(new TextMessage("成功建立websocket-spring连接"));
              //连接后第一次推送当前报警信息
                frontWebData.updateWarnInfo();
                logger.info("用户标识:{},Session:{}", mchNo, session.toString());
            }
            
        }
    
        @Override
        public void handleTextMessage(WebSocketSession session, TextMessage message) {
            logger.info("收到客户端消息:{}", message.getPayload());
            String mchNo = getMchNo(session);
            if (StringUtils.isNotEmpty(mchNo)) {
                // 获取提交过来的消息详情
                logger.debug("收到用户 " + mchNo + "的消息:" + message.toString());
            }
            
            
        }
    
        @Override
        public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        	
        	if (session.isOpen()) {
                session.close();
            }
            logger.info("连接出错");
            users.remove(getMchNo(session));
        	
        }
    
        @Override
        public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        	 logger.info("连接已关闭:" + status);
             users.remove(getMchNo(session));
        }
    
        @Override
        public boolean supportsPartialMessages() {
            return false;
        }
    
        
        /**
         * 发送信息给指定用户
         * @Title: sendMessageToUser 
         * @Description: TODO
         * @Date 2018年8月21日 上午11:01:08 
         * @author OnlyMate
         * @param mchNo
         * @param message
         * @return
         */
        public boolean sendMessageToUser(String mchNo, TextMessage message) {
        	
        	if (users.get(mchNo) == null)
                return false;
            WebSocketSession session = users.get(mchNo);
            logger.info("sendMessage:{} ,msg:{}", session, message.getPayload());
            if (!session.isOpen()) {
                logger.info("客户端:{},已断开连接,发送消息失败", mchNo);
                return false;
            }
            try {
                session.sendMessage(message);
            } catch (IOException e) {
                logger.info("sendMessageToUser method error:{}", e);
                return false;
            }
            return true;
        }
    
        /**
         * 广播信息
         * @Title: sendMessageToAllUsers 
         * @Description: TODO
         * @Date 2018年8月21日 上午11:01:14 
         * @author OnlyMate
         * @param message
         * @return
         */
        public boolean sendMessageToAllUsers(TextMessage message) {
        	boolean allSendSuccess = true;
            Set<String> mchNos = users.keySet();
            WebSocketSession session = null;
            for (String mchNo : mchNos) {
                try {
                    session = users.get(mchNo);
                    if (session.isOpen()) {
                        session.sendMessage(message);
                    }else {
                        logger.info("客户端:{},已断开连接,发送消息失败", mchNo);
                    }
                } catch (IOException e) {
                    logger.info("sendMessageToAllUsers method error:{}", e);
                    allSendSuccess = false;
                }
            }
    
            return allSendSuccess;
        }
        
        /**
         * @Description: TODO 发送信息给相同部门
         * @author xiehui
         * @date 2018/8/25 下午4:49
         */
        public void sendMessageByDepartment(TextMessage message, String departId) {
        	
        	Set<String> mchNos = users.keySet();
            WebSocketSession session = null;
            for (String mchNo : mchNos) {
                try {
                    session = users.get(mchNo);
                    if (session.isOpen() && departId.equals(String.valueOf(session.getAttributes().get("departId")))) {
                    	session.sendMessage(message);
                    }
                } catch (IOException e) {
                    logger.info("sendMessageToAllUsers method error:{}", e);
                }
            }
        }
        
        /**
         * 获取用户标识
         * @Title: getMchNo 
         * @Description: TODO
         * @Date 2018年8月21日 上午11:01:01 
         * @author OnlyMate
         * @param session
         * @return
         */
        private String getMchNo(WebSocketSession session) {
            try {
                String mchNo = session.getAttributes().get(CLIENT_ID).toString();
                return mchNo;
            } catch (Exception e) {
                return null;
            }
        }
    
    	public static Map<String, WebSocketSession> getUsers() {
    		return users;
    	}
        
        
        
    }
  8. 把websocketSession和httpsession对应起来,这样就能根据当前不同的session,定向对websocketSession进行数据返回;在查询资料之后,发现spring中有一个拦截器接口,HandshakeInterceptor,可以实现这个接口,来拦截握手过程,向其中添加属性
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.server.ServerHttpRequest;
    import org.springframework.http.server.ServerHttpResponse;
    import org.springframework.http.server.ServletServerHttpRequest;
    import org.springframework.web.socket.WebSocketHandler;
    import org.springframework.web.socket.server.HandshakeInterceptor;
    
    import com.founder.commons.web.login.dto.LoginUser;
    
    /**
     * WebSocket握手时的拦截器
     * @ClassName: CustomWebSocketInterceptor 
     *
     */
    public class CustomWebSocketInterceptor implements HandshakeInterceptor {
        private Logger logger = LoggerFactory.getLogger(CustomWebSocketInterceptor.class);
        /**
         * 关联HeepSession和WebSocketSession,
         * beforeHandShake方法中的Map参数 就是对应websocketSession里的属性
         */
        @Override
        public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, Map<String, Object> map) throws Exception {
            if (request instanceof ServletServerHttpRequest) {
                logger.info("*****beforeHandshake******");
                HttpServletRequest httpServletRequest = ((ServletServerHttpRequest) request).getServletRequest();
                HttpSession session = httpServletRequest.getSession(true);
                
                if (session != null) {
                	
                	 ///使用userName区分WebSocketHandler,以便定向发送消息
                	LoginUser	systemLoginName =  (LoginUser) session.getAttribute("systemLoginName");   //一般直接保存user实体
                    if (systemLoginName!=null) {
                    	 map.put("sessionId",session.getId());
                    	map.put("username",systemLoginName.getUserName());
                    	map.put("departId", systemLoginName.getDepartmentID());
                    }
                   
                }
            }
            return true;
        }
    
        @Override
        public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {
            logger.info("******afterHandshake******");
        }
    }  
  9. 配置类向Spring中注入handler
    package com.founder.commons.websocket;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.socket.WebSocketHandler;
    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 com.founder.szhd.websocket.CustomWebSocketHandler;
    
    /**
     * websocket的配置类  
     *
     */
    @Configuration
    @EnableWebSocket
    public class CustomWebSocketConfig implements WebSocketConfigurer {
        
    
        @Override
        public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
            registry.addHandler(customWebSocketHandler(), "/webSocketBySpring/customWebSocketHandler.do").addInterceptors(new CustomWebSocketInterceptor());
            registry.addHandler(customWebSocketHandler(), "/sockjs/webSocketBySpring/customWebSocketHandler").addInterceptors(new CustomWebSocketInterceptor()).withSockJS();
        }
    
        @Bean
        public WebSocketHandler customWebSocketHandler() {
            return new CustomWebSocketHandler();
        }
    }  
  10. 前端js中调用
    
    

    var websocket = null;
    //判断当前浏览器是否支持WebSocket
    //判断当前浏览器是否支持WebSocket
    if('WebSocket' in window) {
    websocket = new WebSocket("ws://localhost:8080/xxxx/webSocketBySpring/customWebSocketHandler.do");
    } else if('MozWebSocket' in window) {
    websocket = new MozWebSocket("ws://localhost:8080/xxxx/webSocketBySpring/customWebSocketHandler.do");
    } else {
    websocket = new SockJS("http://localhost:8080/xxxx/sockjs/webSocketBySpring/customWebSocketHandler.do");
    }
    //连接发生错误的回调方法
    websocket.onerror = function () {
    setMessageInnerHTML("WebSocket连接发生错误");
    };

    //连接成功建立的回调方法
    websocket.onopen = function () {
    setMessageInnerHTML("WebSocket连接成功");
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
    setMessageInnerHTML(event.data);
    }

    //连接关闭的回调方法
    websocket.onclose = function () {
    setMessageInnerHTML("WebSocket连接关闭");
    }

    //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
    window.onbeforeunload = function () {
    closeWebSocket();
    }

    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML) {
    alert(innerHTML);
    }

    //关闭WebSocket连接
    function closeWebSocket() {
    websocket.close();
    }

    //发送消息
    function send() {
    var message = "发送消息";
    websocket.send(message);
    }

  11. 补充说明:

    setAllowedOrigins("*")一定要加上,不然只有访问localhost,其他的不予许访问

    setAllowedOrigins(String[] domains),允许指定的域名或IP(含端口号)建立长连接,如果只允许自家域名访问,这里轻松设置。如果不限时使用"*"号,如果指定了域名,则必须要以http或https开头

    经查阅官方文档springwebsocket 4.1.5版本前默认支持跨域访问,之后的版本默认不支持跨域,需要设置

     

    使用withSockJS()的原因:

      一些浏览器中缺少对WebSocket的支持,因此,回退选项是必要的,而Spring框架提供了基于SockJS协议的透明的回退选项。

    SockJS的一大好处在于提供了浏览器兼容性。优先使用原生WebSocket,如果在不支持websocket的浏览器中,会自动降为轮询的方式。 
    除此之外,spring也对socketJS提供了支持。

    如果代码中添加了withSockJS()如下,服务器也会自动降级为轮询。

    registry.addEndpoint("/coordination").withSockJS();

    SockJS的目标是让应用程序使用WebSocket API,但在运行时需要在必要时返回到非WebSocket替代,即无需更改应用程序代码。

    SockJS是为在浏览器中使用而设计的。它使用各种各样的技术支持广泛的浏览器版本。对于SockJS传输类型和浏览器的完整列表,可以看到SockJS客户端页面。 
    传输分为3类:WebSocket、HTTP流和HTTP长轮询(按优秀选择的顺序分为3类)

posted @ 2020-11-24 11:32  爱吃西红柿  阅读(301)  评论(0编辑  收藏  举报