springboot如何使用websocket实现前后端通信

如何使用websocket实现前后端通信

websocket通信是很好玩的,也很有用的的通信方式,使用方式如下:

第一步

由于springboot很好地集成了websocket,所以先在在pom.xml文件中引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

第二步

创建配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;


@Configuration
public class WebsocketConfiguration {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

第三步

创建一个注解式的端点并在其中通过配套注解声明回调方法

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint(value = "/wsdemo")
@Component
public class MyWebSocket {

    /**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
    private static int onlineCount = 0;

    /** concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
     在外部可以获取此连接的所有websocket对象,并能对其触发消息发送功能,我们的定时发送核心功能的实现在与此变量 */
    private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>();

    /**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
    private Session session;

    /**
     * 连接建立成功调用的方法
     *
     * 类似dwr的onpage方法,参考之前文章中demo有
     * */
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        webSocketSet.add(this);     //加入set中
        addOnlineCount();           //在线数加1
        System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
        try {
            sendMessage("连接已建立成功.");
        } catch (Exception e) {
            System.out.println("IO异常");
        }
    }

    /**
     * 连接关闭调用的方法
     *
     * 参考dwrsession摧毁方法
     */
    @OnClose
    public void onClose() {
        webSocketSet.remove(this);  //连接关闭后,将此websocket从set中删除
        subOnlineCount();           //在线数减1
        System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息*/
    @OnMessage
    public void onMessage(String message, Session session) {

        System.out.println("来自客户端的消息:" + message);
        try {
            sendMessage("收到"+message);
            sendMessage("还活着");
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    // 错误提示
    @OnError
    public void onError(Session session, Throwable error) {
        System.out.println("发生错误");
        error.printStackTrace();
    }

    // 发送消息,在定时任务中会调用此方法
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);

    }


    






    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        MyWebSocket.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        MyWebSocket.onlineCount--;
    }



    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public static CopyOnWriteArraySet<MyWebSocket> getWebSocketSet() {
        return webSocketSet;
    }

    public static void setWebSocketSet(CopyOnWriteArraySet<MyWebSocket> webSocketSet) {
        MyWebSocket.webSocketSet = webSocketSet;
    }
}

建立一个发送的方法其他地方可以调用此方法进行发送上面定义的sendMessage方法,方法里面的返回对象可以自己定义,此处定义的是String类型,返回的是字符串

此处还需要用到对象转json,使用的是

 <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
import com.google.gson.Gson;

import com.ruoyi.data.domain.Datas;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;

@Component
public class WebSocketUtils {
    public static void sendMessage(List<Datas> list) {

        Gson gson = new Gson();
        
//遍历一组WebSocket连接,并使用sendMessage方法向每个连接发送JSON序列化列表。如果在此过程中抛出IOException,则会将堆栈跟踪打印到控制台。
        CopyOnWriteArraySet<MyWebSocket> webSocketSet =
                MyWebSocket.getWebSocketSet();

        webSocketSet.forEach(c -> {
            try {
                c.sendMessage(gson.toJson(list));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}

定义一个切面每次传过来一条数据我就查询跟这条数据相关的内容返回给客户端,传一条返回集合只有一个数据,传更多返回集合有多条数据,当换一批的时候就会返回一个全新的集合

import com.ruoyi.data.domain.Datas;
import com.ruoyi.data.mapper.DataMapper;
import com.ruoyi.data.utils.WebSocketUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Aspect
@Component
public class SendMessageAspect {
    @Autowired
    private DataMapper dataMapper;
    @Pointcut("execution(public * com.ruoyi.data.service.impl.*.*(..))")
    public void LogAspect() {
    }

    @Before("LogAspect()")
    public void doBefore(JoinPoint joinPoint) {
    }

    @After("LogAspect()")
    public void doAfter(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Datas datas = new Datas();
        //此处是提取我所需要的数据指定格式并将提取出来的数据存到data里面
        Pattern pattern = Pattern.compile("uid=(\\w+-\\w+-\\w+-\\w+-\\w+), camID=(\\d+), boxID=(\\d+)");
        Matcher matcher = pattern.matcher(args[0].toString());
        
        
        if (matcher.find()) {
            String uid = matcher.group(1);
            String camID = matcher.group(2);
            String boxID = matcher.group(3);

            datas.setBatch(uid);
            datas.setCamID(Integer.parseInt(camID));
            datas.setBoxID(Integer.parseInt(boxID));
        }
        //list是我查询到的数据返回给客户端使用的是我定义的一个静态方法
        List<Datas> list=dataMapper.selectByboxid(datas);
        System.out.println(list);
        WebSocketUtils.sendMessage(list);
    }

    @AfterReturning("LogAspect()")
    public void doAfterReturning(JoinPoint joinPoint) {
        System.out.println("doAfterReturning");
    }

    @AfterThrowing("LogAspect()")
    public void deAfterThrowing(JoinPoint joinPoint) {
        System.out.println("deAfterThrowing");
    }

    @Around("LogAspect()")
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("deAround");
        return joinPoint.proceed();
    }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值