WebSocket

maven

xml 复制代码
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

配置类

java 复制代码
package com.sky.config;

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

/**
 * WebSocket配置类,用于注册WebSocket的Bean
 */
@Configuration
public class WebSocketConfiguration {

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

}

WebSocketServer

java 复制代码
package com.sky.websocket;

import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * WebSocket服务
 */
@Component
@ServerEndpoint("/ws/{sid}")
public class WebSocketServer {

    //存放会话对象
    private static Map<String, Session> sessionMap = new HashMap();

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("sid") String sid) {
        System.out.println("客户端:" + sid + "建立连接");
        sessionMap.put(sid, session);
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, @PathParam("sid") String sid) {
        System.out.println("收到来自客户端:" + sid + "的信息:" + message);
    }

    /**
     * 连接关闭调用的方法
     *
     * @param sid
     */
    @OnClose
    public void onClose(@PathParam("sid") String sid) {
        System.out.println("连接断开:" + sid);
        sessionMap.remove(sid);
    }

    /**
     * 群发
     *
     * @param message
     */
    public void sendToAllClient(String message) {
        Collection<Session> sessions = sessionMap.values();
        for (Session session : sessions) {
            try {
                //服务器向客户端发送消息
                session.getBasicRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

来单提醒

用户下单成功后,需要第一时间通知外卖商家,通知有两种:语音弹幕 弹出提示框

消息分为两种:来单提醒 和 客户催单

在paysuccess后面加

java 复制代码
//通过websocket向客户端推送数据  type orderId  content
Map map=new HashMap<>();
map.put("type",1);//1  表示来电提醒  2表示客户催单
map.put("orderId",ordersDB.getId());
map.put("content","订单号: "+outTradeNo);
String json= JSON.toJSONString(map);
webSocketServer.sendToAllClient(json);

客户催单

Controller层

java 复制代码
/*
\* 客户催单
\* */
@GetMapping("/reminder/{id}")
@ApiOperation("客户催单")
public Result reminder(@PathVariable("id") Long id){
       orderService.reminder(id);
  return Result.success();
}

service层

java 复制代码
/*

\* 客户催单

\* */
@Override
public void reminder(Long id) {
  // 根据id查询订单
  Orders ordersDB = orderMapper.getById(id);
  // 校验订单是否存在,并且状态为4
  if (ordersDB == null) {
     throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
  }
  Map map=new HashMap<>();
  map.put("type",2);//1表示来单提醒  2表示客户催单
  map.put("orderId",id);
  map.put("content","订单号: "+ordersDB.getId());
  //调用webSocket向客户端浏览器发送消息
  webSocketServer.sendToAllClient(JSON.toJSONString(map));

}
相关推荐
旺仔.29144 分钟前
Linux 信号详解
linux·运维·网络
平生幻2 小时前
TCP协议与UDP协议的区别
网络协议·tcp/ip·udp
源远流长jerry4 小时前
在 Ubuntu 22.04 上配置 Soft-RoCE 并运行 RDMA 测试程序
linux·服务器·网络·tcp/ip·ubuntu·架构·ip
虾..4 小时前
UDP协议
网络·网络协议·udp
w-w0w-w5 小时前
Unix网络编程
服务器·网络·unix
未知鱼5 小时前
Python安全开发之子域名扫描器(含详细注释)
网络·python·安全·web安全·网络安全
寂柒5 小时前
序列化与反序列化
linux·网络
志栋智能6 小时前
超自动化巡检:应对复杂IT环境的必然选择
运维·网络·安全·web安全·自动化
上海云盾-小余7 小时前
云主机安全加固:从系统、网络到应用的零信任配置
网络·安全·php
QCzblack8 小时前
见面考复现
网络