webSocket demo
This commit is contained in:
parent
3172289d4f
commit
2df7b27a29
@ -84,6 +84,17 @@
|
||||
<version>5.7.5</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.websocket</groupId>
|
||||
<artifactId>javax.websocket-api</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
package com.xhpc.order.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* @author yuyang
|
||||
* @date 2021/8/9 17:13
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class WebSocketConfig {
|
||||
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,122 @@
|
||||
package com.xhpc.order.controller;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import javax.websocket.OnClose;
|
||||
import javax.websocket.OnError;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
/**
|
||||
* @author yuyang
|
||||
* @date 2021/8/9 14:33
|
||||
*/
|
||||
@Component
|
||||
@ServerEndpoint(value="/websocket/{userId}")
|
||||
public class WebSocketController {
|
||||
|
||||
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
|
||||
private static int onlineCount = 0;
|
||||
|
||||
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
|
||||
private static CopyOnWriteArraySet<WebSocketController> webSocketSet = new CopyOnWriteArraySet<WebSocketController>();
|
||||
|
||||
//与某个客户端的连接会话,需要通过它来给客户端发送数据
|
||||
private Session session;
|
||||
//连接用户id
|
||||
private String userId;
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
* @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(@PathParam("userId") String userId,Session session){
|
||||
this.userId =userId;
|
||||
this.session = session;
|
||||
webSocketSet.add(this); //加入set中
|
||||
addOnlineCount(); //在线数加1
|
||||
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭调用的方法
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(){
|
||||
webSocketSet.remove(this); //从set中删除
|
||||
subOnlineCount(); //在线数减1
|
||||
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
* @param message 客户端发送过来的消息
|
||||
* @param session 可选的参数
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(String message, Session session) {
|
||||
System.out.println("收到:" + this.userId+"的消息");
|
||||
System.out.println("来自客户端的消息:" + message);
|
||||
try {
|
||||
message ="{\n" +
|
||||
" \"msg\": \"操作成功\",\n" +
|
||||
" \"code\": 200,\n" +
|
||||
" \"data\": {\n" +
|
||||
" \"amountCharged\": 50.0,\n" +
|
||||
" \"gunNumber\": \"1\",\n" +
|
||||
" \"balance\": 10.00,\n" +
|
||||
" \"chargingOrderId\": 3,\n" +
|
||||
" \"soc\": \"12\",\n" +
|
||||
" \"chargingTime\": \"21\",\n" +
|
||||
" \"realTimeOrderId\": 8,\n" +
|
||||
" \"electricCurrent\": 1.0,\n" +
|
||||
" \"power\": null,\n" +
|
||||
" \"chargingDegree\": 10.25,\n" +
|
||||
" \"voltage\": 45.0,\n" +
|
||||
" \"remainingTime\": \"45\"\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
this.session.getBasicRemote().sendText(message);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发生错误时调用
|
||||
* @param session
|
||||
* @param error
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Session session, Throwable error){
|
||||
System.out.println("发生错误");
|
||||
error.printStackTrace();
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
|
||||
* @param message
|
||||
* @throws IOException
|
||||
*/
|
||||
public void sendMessage(String message) throws IOException{
|
||||
this.session.getBasicRemote().sendText(message);
|
||||
//this.session.getAsyncRemote().sendText(message);
|
||||
}
|
||||
|
||||
public static synchronized int getOnlineCount() {
|
||||
return onlineCount;
|
||||
}
|
||||
|
||||
public static synchronized void addOnlineCount() {
|
||||
WebSocketController.onlineCount++;
|
||||
}
|
||||
|
||||
public static synchronized void subOnlineCount() {
|
||||
WebSocketController.onlineCount--;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
package com.xhpc.order.domain;
|
||||
|
||||
import com.xhpc.common.core.web.domain.BaseEntity;
|
||||
|
||||
/**
|
||||
* @author yuyang
|
||||
* @date 2021/8/9 16:01
|
||||
*/
|
||||
public class XhpcChargeOrderCurrent extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 订单电流实时数据
|
||||
*/
|
||||
private Long chargeOrderCurrentId;
|
||||
/**
|
||||
* 订单id
|
||||
*/
|
||||
private Long chargeOrderId;
|
||||
/**
|
||||
* 电流实时走向
|
||||
*/
|
||||
private String current;
|
||||
|
||||
/**
|
||||
* (状态)
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 0代表存在 1代表删除
|
||||
*/
|
||||
private Integer delFlag;
|
||||
|
||||
public Long getChargeOrderCurrentId() {
|
||||
|
||||
return chargeOrderCurrentId;
|
||||
}
|
||||
|
||||
public void setChargeOrderCurrentId(Long chargeOrderCurrentId) {
|
||||
|
||||
this.chargeOrderCurrentId = chargeOrderCurrentId;
|
||||
}
|
||||
|
||||
public Long getChargeOrderId() {
|
||||
|
||||
return chargeOrderId;
|
||||
}
|
||||
|
||||
public void setChargeOrderId(Long chargeOrderId) {
|
||||
|
||||
this.chargeOrderId = chargeOrderId;
|
||||
}
|
||||
|
||||
public String getCurrent() {
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
public void setCurrent(String current) {
|
||||
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Integer getDelFlag() {
|
||||
|
||||
return delFlag;
|
||||
}
|
||||
|
||||
public void setDelFlag(Integer delFlag) {
|
||||
|
||||
this.delFlag = delFlag;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,86 @@
|
||||
package com.xhpc.order.domain;
|
||||
|
||||
import com.xhpc.common.core.web.domain.BaseEntity;
|
||||
|
||||
/**
|
||||
* @author yuyang
|
||||
* @date 2021/8/9 15:56
|
||||
*/
|
||||
public class XhpcChargeOrderSoc extends BaseEntity {
|
||||
|
||||
|
||||
/**
|
||||
* 订单SOC实时数据
|
||||
*/
|
||||
private Long chargeOrderSocId;
|
||||
/**
|
||||
* 订单id
|
||||
*/
|
||||
private Long chargeOrderId;
|
||||
/**
|
||||
* soc 实时走向
|
||||
*/
|
||||
private String soc;
|
||||
|
||||
/**
|
||||
* (状态)
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 0代表存在 1代表删除
|
||||
*/
|
||||
private Integer delFlag;
|
||||
|
||||
|
||||
public Long getChargeOrderSocId() {
|
||||
|
||||
return chargeOrderSocId;
|
||||
}
|
||||
|
||||
public void setChargeOrderSocId(Long chargeOrderSocId) {
|
||||
|
||||
this.chargeOrderSocId = chargeOrderSocId;
|
||||
}
|
||||
|
||||
public Long getChargeOrderId() {
|
||||
|
||||
return chargeOrderId;
|
||||
}
|
||||
|
||||
public void setChargeOrderId(Long chargeOrderId) {
|
||||
|
||||
this.chargeOrderId = chargeOrderId;
|
||||
}
|
||||
|
||||
public String getSoc() {
|
||||
|
||||
return soc;
|
||||
}
|
||||
|
||||
public void setSoc(String soc) {
|
||||
|
||||
this.soc = soc;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Integer getDelFlag() {
|
||||
|
||||
return delFlag;
|
||||
}
|
||||
|
||||
public void setDelFlag(Integer delFlag) {
|
||||
|
||||
this.delFlag = delFlag;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
package com.xhpc.order.domain;
|
||||
|
||||
import com.xhpc.common.core.web.domain.BaseEntity;
|
||||
|
||||
/**
|
||||
* @author yuyang
|
||||
* @date 2021/8/9 16:02
|
||||
*/
|
||||
public class XhpcChargeOrderVoltage extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 订单电压实时数据
|
||||
*/
|
||||
private Long chargeOrderVoltageId;
|
||||
/**
|
||||
* 订单id
|
||||
*/
|
||||
private Long chargeOrderId;
|
||||
/**
|
||||
* 电压实时走向
|
||||
*/
|
||||
private String voltage;
|
||||
|
||||
/**
|
||||
* (状态)
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 0代表存在 1代表删除
|
||||
*/
|
||||
private Integer delFlag;
|
||||
|
||||
public Long getChargeOrderVoltageId() {
|
||||
|
||||
return chargeOrderVoltageId;
|
||||
}
|
||||
|
||||
public void setChargeOrderVoltageId(Long chargeOrderVoltageId) {
|
||||
|
||||
this.chargeOrderVoltageId = chargeOrderVoltageId;
|
||||
}
|
||||
|
||||
public Long getChargeOrderId() {
|
||||
|
||||
return chargeOrderId;
|
||||
}
|
||||
|
||||
public void setChargeOrderId(Long chargeOrderId) {
|
||||
|
||||
this.chargeOrderId = chargeOrderId;
|
||||
}
|
||||
|
||||
public String getVoltage() {
|
||||
|
||||
return voltage;
|
||||
}
|
||||
|
||||
public void setVoltage(String voltage) {
|
||||
|
||||
this.voltage = voltage;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Integer getDelFlag() {
|
||||
|
||||
return delFlag;
|
||||
}
|
||||
|
||||
public void setDelFlag(Integer delFlag) {
|
||||
|
||||
this.delFlag = delFlag;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.xhpc.order.mapper;
|
||||
|
||||
import com.xhpc.order.domain.XhpcChargeOrderSoc;
|
||||
import com.xhpc.order.domain.XhpcRealTimeOrder;
|
||||
|
||||
/**
|
||||
@ -14,4 +15,14 @@ public interface XhpcRealTimeOrderMapper {
|
||||
* @return
|
||||
*/
|
||||
int addXhpcRealTimeOrder(XhpcRealTimeOrder xhpcRealTimeOrder);
|
||||
|
||||
|
||||
/**
|
||||
* 添加订单实时SOC
|
||||
* @param xhpcChargeOrderSoc
|
||||
* @return
|
||||
*/
|
||||
int addSOC(XhpcChargeOrderSoc xhpcChargeOrderSoc);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -24,6 +24,8 @@ public class XhpcRealTimeOrderServiceImpl implements IXhpcRealTimeOrderService {
|
||||
public void addXhpcRealTimeOrder(String orderNo, Integer status) {
|
||||
|
||||
|
||||
//获取redis 数据
|
||||
|
||||
|
||||
//记录电流、电压、soc实时记录
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user