Websocket实现消息推送基于SpringBoot+uniapp实现

本文介绍了如何在SpringBoot项目中结合uniapp使用WebSocket进行双向通信,包括WebSocket的工作原理、依赖导入、服务端配置和控制层逻辑,以及uniapp客户端的连接和消息处理示例。


前言

      WebSocket协议使用标准的HTTP握手过程来建立连接,然后通过在已建立的连接上进行数据交换。这使得WebSocket非常适用于实时聊天、在线游戏、实时数据更新等场景。

        本文就介绍了Websocket实现消息推送基于SpringBoot+uniapp实现


一、WebSocket是什么?

         WebSocket是一种用于在客户端和服务器之间进行双向通信的网络协议。它建立在HTTP协议上,通过在单个TCP连接上提供全双工通信,允许服务器主动向客户端推送数据。

        与传统的HTTP请求-响应模型不同,WebSocket允许服务器和客户端之间实时地进行双向通信。一旦建立了WebSocket连接,客户端和服务器就可以通过发送消息来直接进行通信,而不需要每次都发起新的HTTP请求。

二、使用步骤

1.导入部分依赖

代码如下:

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.72</version>
        </dependency>

2.服务端代码(部分,实体类请根据需求自行创建)

注意:实体类请根据需求自行创建

代码如下:

package com.recovery.garbage.websocket.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;


@Configuration
public class WebSocketStompConfig {
    //这个bean的注册,用于扫描带有@ServerEndpoint的注解成为websocket  ,如果你使用外置的tomcat就不需要该配置文件
    @Bean
    public ServerEndpointExporter serverEndpointExporter()
    {
        return new ServerEndpointExporter();
    }
}

Service层

package com.recovery.garbage.websocket.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.recovery.garbage.Bean.Chat;
import com.recovery.garbage.Bean.Order;
import com.recovery.garbage.Service.Mapper.ChatMapper;
import com.recovery.garbage.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint(value = "/connectWebSocket/{status}/{userId}")
//status=0用户 1=骑手
public class WebSocket {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 在线人数
     */
    public static int onlineNumber = 0;
    /**
     * 以用户的姓名为key,WebSocket为对象保存起来
     */
    private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
    /**
     * 会话
     */
    private Session session;
    /**
     * 用户名称
     */
    private String userId;
    /**
     * 建立连接
     *
     * @param session
     */

    private int status;

    @Autowired
    private ChatMapper chatMapper;

    @OnOpen
    public void onOpen(@PathParam("status") int status,@PathParam("userId") String userId, Session session)
    {
        onlineNumber++;

        logger.info("现在来连接的客户id:"+session.getId()+"用户名:"+userId);
        this.userId = userId;
        this.session = session;
        this.status=status;

        //  logger.info("有新连接加入! 当前在线人数" + onlineNumber);
        //messageType 1代表上线 2代表下线 3代表在线名单 4代表普通消息
        //先给所有人发送通知,说我上线了
        Map<String,Object> map1 = Maps.newHashMap();
        map1.put("messageType",1);
        map1.put("userId",userId);
        if(this.status==0){
            map1.put("status",0);
        }else if(this.status==1){
            map1.put("status",1);
        }

        //sendMessageAll(JSON.toJSONString(map1),userId);

        //把自己的信息加入到map当中去
        clients.put(userId, this);
        logger.info("有连接关闭! 当前在线人数" + clients.size());
        //给自己发一条消息:告诉自己现在都有谁在线
        Map<String,Object> map2 = Maps.newHashMap();
        map2.put("messageType",3);
        //移除掉自己
        Set<String> set = clients.keySet();
        map2.put("onlineUsers",set);
        //sendMessageTo(JSON.toJSONString(map2),userId);
    }

    @OnError
    public void onError(Session session, Throwable error) {
        logger.info("服务端发生了错误"+error.getMessage());
        //error.printStackTrace();
    }
    /**
     * 连接关闭
     */
    @OnClose
    public void onClose()
    {
        onlineNumber--;
        //webSockets.remove(this);
        clients.remove(userId);
        try {
            //messageType 1代表上线 2代表下线 3代表在线名单  4代表普通消息
            Map<String,Object> map1 = Maps.newHashMap();
            map1.put("messageType",2);
            map1.put("onlineUsers",clients.keySet());
            map1.put("userId",userId);
            sendMessageAll(JSON.toJSONString(map1),userId);
        }
        catch (IOException e){
            logger.info(userId+"下线的时候通知所有人发生了错误");
        }
        //logger.info("有连接关闭! 当前在线人数" + onlineNumber);
        logger.info("有连接关闭! 当前在线人数" + clients.size());
    }

    /**
     * 收到客户端的消息
     *
     * @param message 消息
     * @param session 会话
     */
    @OnMessage
    public void onMessage(String message, Session session)
    {
        try {
            logger.info("来自客户端消息:" + message+"客户端的id是:"+session.getId());

            System.out.println("------------  :"+message);

            JSONObject jsonObject = JSON.parseObject(message);
            String textMessage = jsonObject.getString("message");
            String fromuserId = jsonObject.getString("userId");
            String touserId = jsonObject.getString("to");
            //如果不是发给所有,那么就发给某一个人
            //messageType 1代表上线 2代表下线 3代表在线名单  4代表普通消息
            Map<String,Object> map1 = Maps.newHashMap();
            map1.put("messageType",4);
            map1.put("textMessage",textMessage);
            map1.put("fromuserId",fromuserId);
            if(touserId.equals("All")){
                map1.put("touserId","所有人");
                sendMessageAll(JSON.toJSONString(map1),fromuserId);
            }
            else{
                map1.put("touserId",touserId);
                System.out.println("开始推送消息给"+touserId);
                sendMessageTo(JSON.toJSONString(map1),touserId);
            }
        }
        catch (Exception e){

            e.printStackTrace();
            logger.info("发生了错误了");
        }

    }


    public void sendMessageTo(String message, String TouserId) throws IOException {
        for (WebSocket item : clients.values()) {
            if (item.userId.equals(TouserId) ) {
                item.session.getAsyncRemote().sendText(message);
                break;
            }
        }
    }


    public void AcceptOrderSendFromId(String orderId,String fromId){
        for (WebSocket item : clients.values()) {
            //    System.out.println("在线人员名单  :"+item.userId.toString());
            if (item.userId.equals(fromId) ) {
                item.session.getAsyncRemote().sendText(fromId);
                break;
            }
        }

    }


    public void sendOrder(Order order) throws IOException {
        for (WebSocket item : clients.values()) {
            if(item.status==1) {
                System.out.println(JSON.toJSONString(order));
                item.session.getAsyncRemote().sendText(JSON.toJSONString(order));
            }
        }
    }



    public ResultUtil sendMessageToChat(String FromUserId ,String message, String TouserId) throws IOException {
        Chat chatEn=new Chat();
        chatEn.setFromOpenId(FromUserId);
        chatEn.setContent(message);
        chatEn.setToOpenId(TouserId);
        for (WebSocket item : clients.values()) {
            //    System.out.println("在线人员名单  :"+item.userId.toString());
            if (item.userId.equals(TouserId) ) {
                item.session.getAsyncRemote().sendText(message);
                chatEn.setState(1);
                chatMapper.insert(chatEn);
                logger.info("插入成功");
                //webSocket.sendMessageToChat(FormUserid,message,TouserId);
                return ResultUtil.sucess("用户在线");
            }
        }
        chatEn.setState(0);
        chatMapper.insert(chatEn);
        //webSocket.sendMessageToChat(FormUserid,message,TouserId);
        logger.info("插入成功");
        logger.info("用户不在线:"+TouserId);
        return ResultUtil.sucess("用户不在线");
    }




    public void sendMessageAll(String message,String FromuserId) throws IOException {
        for (WebSocket item : clients.values()) {
            item.session.getAsyncRemote().sendText(message);
        }
    }

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

}

Controller层

package com.recovery.garbage.websocket.controller;

import com.recovery.garbage.Bean.Order;
import com.recovery.garbage.websocket.service.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
public class websocketController {
    @Autowired
    WebSocket webSocket;


    @GetMapping("/sendTo")
    public String sendTo(@RequestParam("userId") String userId, @RequestParam("msg") String msg) throws IOException {
        webSocket.sendMessageTo(msg,userId);
        return "推送成功";
    }

    @PostMapping("/sendOrder")
    public String sendOrder(@RequestBody Order order) throws IOException {
        System.out.println(order);
        webSocket.sendOrder(order);
        return "推送订单";
    }



    @PostMapping("/sendAll")
    public String sendAll(@RequestParam("fromId") String fromId) throws IOException {
        System.out.println(fromId);

        webSocket.sendMessageAll("接收", fromId);
        return "推送全部人";
    }

    @GetMapping("/acceptOrder")
    public String acceptOrder(@RequestParam("fromId") String fromId,@RequestParam("orderId")String orderId){
        webSocket.AcceptOrderSendFromId(orderId, fromId);
        return "已接受订单:"+orderId;
    }





}

3.Uniapp代码(部分,根据需求自行修改)

			connectionWebSocket() {
				let that = this
				//连接
				this.socketTask = uni.connectSocket({
					url: WebScocket_URL+uni.getStorageSync("UserInfo").open_id,
					success() {
						console.log('websocket连接成功')
					}

				});
				//打开websocket回调1
				uni.onSocketOpen(function(res) {
		
					console.log('消息连接成功')
					//showMsg('消息连接成功');
					// 每隔5秒钟发送一次心跳,避免websocket连接因超时而自动断开
				setInterval(function() {
						uni.sendSocketMessage({
						data: '心跳检测'
						});
					}, 30 * 1000);
				});
				//连接失败回调
				uni.onSocketError(function(res) {
					//uni.setStorageSync(config.cachePrefix + 'webSocketOnLine', false);
					console.log("消息连接失败")
					// showMsg('消息连接失败');
				});
				//关闭websocket打印
				uni.onSocketClose(function(res) {
					//uni.setStorageSync(config.cachePrefix + 'webSocketOnLine', false);
					// showMsg('消息连接已关闭');
				});
				//服务端过来内容之后打印
				uni.onSocketMessage(function(res) {
					console.log(res);
					
					console.log(that)
					console.log(that.$refs)
					that.$refs.uNotify.show({
						top: 0,
						type: 'success',
						color: '#000',
						bgColor: '#aaff7f',
						message: res.data,
						duration: 2000,
						fontSize: 10,
						//safeAreaInsetTop:true
					})
					
				});
			}


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

### Java SpringBoot WebSocket Uniapp 实现 AI 问答模型教程 #### 集成WebSocketSpring Boot项目中 为了实现实时通信功能,在Java Spring Boot项目中可以利用WebSocket技术。通过引入依赖并配置相应的组件来完成这一目标。 在`pom.xml`文件里加入如下依赖项[^1]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 创建一个WebSocket配置类用于定义连接路径以及消息处理逻辑: ```java @Configuration @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("/ws").withSockJS(); } } ``` 接着编写控制器接收客户端发送的消息并向其推送响应数据: ```java @Controller public class ChatController { private final SimpMessagingTemplate template; @Autowired public ChatController(SimpMessagingTemplate template){ this.template = template; } @MessageMapping("/chat") public void handleChat(@Payload String message, Principal principal) throws Exception{ // 处理接收到的信息,比如调用AI接口获取回复内容 String reply = "This is a simulated response from the server."; // 将服务器返回的内容推送给特定用户或其他订阅者 template.convertAndSendToUser(principal.getName(), "/queue/reply",reply); } } ``` #### 使用UniApp与后端交互 对于前端部分采用UniApp框架开发跨平台应用。安装必要的插件之后可以在页面内初始化Socket.IO实例并与远端建立联系。 首先确保已经安装了`uni-app`环境,并且可以通过npm命令行工具管理包资源。执行下面指令添加所需库文件: ```bash npm install socket.io-client --save ``` 随后修改Vue单页应用程序中的JavaScript代码片段以便于同WebScoket服务对接: ```javascript import io from 'socket.io-client'; export default { data() { return { socket: null, messages: [] }; }, mounted(){ const url='http://localhost:8080/ws'; this.socket=io(url,{ transports:['websocket'] }); this.socket.on('connect',()=>{ console.log('Connected to websocket'); this.socket.emit('subscribe',{ channel:'/user/topic/messages' }); this.socket.on('/user/queue/reply',(data)=>{ this.messages.push(data); }); }); }, methods:{ sendMessage(messageText){ if(!messageText.trim())return ; let msg={ content:messageText, sender:'client', timestamp:new Date().toISOString() }; this.messages.push(msg); this.socket.emit('sendMessage',{ destination:'/app/chat', body:JSON.stringify({content:messageText}) }); } } }; ``` 以上就是关于如何在一个基于Java Spring Boot的项目里面集成WebSocket协议从而支持即时通讯特性;同时也展示了怎样借助Uniappp构建移动设备上的聊天界面并通过它向远程API发起请求的过程[^2]。 #### 构建AI问答模型 针对AI问答系统的搭建,则涉及到自然语言处理(NLP)领域内的多项技能和技术栈的选择。考虑到实际应用场景的需求差异较大,这里仅提供一种较为通用的方式——即使用预训练的语言模型作为基础架构来进行微调操作以适应具体任务特点。 目前比较流行的开源解决方案有Hugging Face Transformers库所提供的BERT系列模型及其变体形式(RoBERTa、DistilBERT等)。这些模型已经在大规模语料上进行了充分的学习,因此可以直接拿来当作初始参数设置的一部分,再依据自有数据集进一步优化权重分布情况得到最终版本的应用程序。 具体的实践步骤包括但不限于以下几个方面: - 数据收集:整理好待解决问题域下的对话样本集合; - 文本清理:去除噪声干扰因素如HTML标签、特殊字符等; - 特征提取:将原始输入转化为适合喂给神经网络的形式; - 训练过程:调整超参直至获得满意的性能指标为止; - 测试评估:验证模型泛化能力是否满足预期效果的要求。 当完成了上述环节的工作以后就可以把训练好的算法部署至云端或者本地环境中供在线预测调用了。此时只需简单改造之前提到过的Spring Boot Controller层即可轻松接入新的智能化模块[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值