spring+websocket的整合实例(超详细,可使用)

定义:
WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端
上面是百度官网的介绍,我主要使用websocket的目的是为了,能让服务器能够主动推送消息给浏览器,而不是之前通过ajax或者长轮循不断触发服务器。

我通过学习整合了一个简单可用通讯的实例。好了,废话不多说,直接上内容。

整合步骤:
由于我用的maven项目,所以先通过pom.xml进行引包。

**第一步:**pom.xml引包
在这里需要注意2点:1、spring相关的jar包必须是版本4以上的 2、websoket引入的jar包版本号和spring的版本号要一直,如果不一致的话回出现:NoClassDefFoundError: org/springframework/web/cors/CorsProcessor,这样框架异常。

添加的spring的支持包:


    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-coreartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-beansartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>
    <dependency>
         <groupId>org.springframeworkgroupId>
         <artifactId>spring-txartifactId>
         <version>4.2.4.RELEASEversion>
        dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-context-supportartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>

    <dependency>  
      <groupId>commons-langgroupId>  
      <artifactId>commons-langartifactId>  
      <version>2.6version>  
      <scope>providedscope>  
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aspectsartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>4.2.4.RELEASEversion>
    dependency>

添加websocket的jar包


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-websocketartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-messagingartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>

第二步:配置spring-websocket.xml文件

这里注意的是:xsi:schemaLocation属性后面俩行一定要有。

  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:websocket="http://www.springframework.org/schema/websocket"  
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
       http://www.springframework.org/schema/websocket  
       http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">  

    <bean id="websocket" class="com.web.common.util.websocket.WebSocketHander" />  

    <websocket:handlers>  
        <websocket:mapping path="/echo" handler="websocket" />  
        <websocket:handshake-interceptors>  
            <bean class="com.web.common.util.websocket.HandshakeInterceptor" />  
        websocket:handshake-interceptors>  
    websocket:handlers>  

beans> 

在applicationContext.xml中引入spring-websocket.xml文件


     <import resource="spring-websocket.xml"/>

在web.xml中引入applicationContext.xml


    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

第三步:编写代码

package com.web.common.util.websocket;

import org.apache.shiro.session.Session;
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 javax.servlet.http.HttpServletRequest;

import java.util.Map;

/**
 * 拦截器
 * 
 * @author wangguan,
 * @date 2017年11月22日 上午9:33:46,
 * @version argType
 */

public class HandshakeInterceptor implements org.springframework.web.socket.server.HandshakeInterceptor{
    Session session;
    // 初次握手访问前
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse serverHttpResponse,
            WebSocketHandler webSocketHandler, Map map) throws Exception {
        if (request instanceof ServletServerHttpRequest) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String sessionId=servletRequest.getSession().getId();//获取浏览器的sessionid
            String username=(String)servletRequest.getSession().getAttribute("name");
            System.out.println("获取session里面的name-------------------"+username);
            // 使用userName区分WebSocketHandler,以便定向发送消息
            /* String userName = (String)*/
            //session.getAttribute("WEBSOCKET_USERNAME");
            map.put("WEBSOCKET_USERNAME", username);
            servletRequest.getSession().setAttribute("WEBSOCKET_USERNAME", username);
        }
        return true;
    }

    // 初次握手访问后
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse,
            WebSocketHandler webSocketHandler, Exception e) {

    }
}

WebSocketHander 实现WebSocketHandler

package com.web.common.util.websocket;

import org.apache.log4j.Logger;
import org.springframework.web.socket.*;

import java.io.IOException;
import java.util.ArrayList;

public class WebSocketHander implements WebSocketHandler {

    private static final Logger logger = Logger.getLogger(WebSocketHander.class);

    private static final ArrayList users = new ArrayList();

    private String userName="";

    // 初次链接成功执行
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        logger.debug("链接成功......");
        users.add(session);
        userName = (String) session.getAttributes().get("WEBSOCKET_USERNAME");
        System.out.println("第一次连接获取的id--"+userName);
        if (userName != null) {
            // 查询未读消息
            int count = 5;
            session.sendMessage(new TextMessage(count + ""));
        }
    }

    // 接受消息处理消息
    public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage webSocketMessage)
            throws Exception {
        //sendMessageToUsers(new TextMessage(webSocketMessage.getPayload() + ""));
        sendMessageToUser(userName, new TextMessage(webSocketMessage.getPayload() + ""));

    }

    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {
        if (webSocketSession.isOpen()) {
            webSocketSession.close();
        }
        logger.debug("链接出错,关闭链接......");
        users.remove(webSocketSession);
    }

    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {
        logger.debug("链接关闭......" + closeStatus.toString());
        users.remove(webSocketSession);
    }

    public boolean supportsPartialMessages() {
        return false;
    }

    /**
     * 给所有在线用户发送消息
     *
     * @param message
     */
    public void sendMessageToUsers(TextMessage message) {
        for (WebSocketSession user : users) {
            System.out.println(user.getAttributes().get("WEBSOCKET_USERNAME"));
            try {
                if (user.isOpen()) {
                    user.sendMessage(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 给某个用户发送消息,模拟给admin发信息
     *
     * @param userName
     * @param message
     */
    public void sendMessageToUser(String userName, TextMessage message) {
        for (WebSocketSession user : users) {
            System.out.println("从session里面获取的id"+user.getAttributes().get("WEBSOCKET_USERNAME"));
            if (user.getAttributes().get("WEBSOCKET_USERNAME").equals("admin")) {
                try {
                    if (user.isOpen()) {
                        user.sendMessage(message);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}

启动项目:
我做了一个模拟,用俩个用户登录,让其中一个用户发消息,另外一个用户接受到消息。所以在我HandshakeInterceptor 用session去用户的登录名用户定向发送消息。(可以自己写一个login的controller,然后把loginName存入的session中,这样HandshakeInterceptor 中才能取到name值。)

我分别用360浏览器和火狐浏览器,模拟俩个登录用户,admin和tea
spring+websocket的整合实例(超详细,可使用)_第1张图片

由tea向admin发送消息
spring+websocket的整合实例(超详细,可使用)_第2张图片

你可能感兴趣的:(通讯)