webSocket Demo,小程序异常订单接口

This commit is contained in:
yuyang 2021-08-09 21:18:02 +08:00
parent 1f581be355
commit 9e6be0a359
7 changed files with 92 additions and 24 deletions

View File

@ -0,0 +1,30 @@
package com.xhpc.order.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* @author yuyang
* @date 2021/8/9 20:31
*/
@Component
@Lazy(false)
public class ApplicationContextRegister implements ApplicationContextAware {
private static ApplicationContext APPLICATION_CONTEXT;
/**
* 设置spring上下文 * * @param applicationContext spring上下文 * @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
APPLICATION_CONTEXT = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return APPLICATION_CONTEXT;
}
}

View File

@ -1,6 +1,13 @@
package com.xhpc.order.controller; package com.xhpc.order.controller;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.order.config.ApplicationContextRegister;
import com.xhpc.order.service.IHxpcChargeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.SpringConfigurator;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
@ -19,22 +26,28 @@ import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value="/websocket/{userId}") @ServerEndpoint(value="/websocket/{userId}")
public class WebSocketController { public class WebSocketController {
//静态变量用来记录当前在线连接数应该把它设计成线程安全的 /**
* 静态变量用来记录当前在线连接数应该把它设计成线程安全的
*/
private static int onlineCount = 0; private static int onlineCount = 0;
/**
//concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象若要实现服务端与单一客户端通信的话可以使用Map来存放其中Key可以为用户标识 * concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象若要实现服务端与单一客户端通信的话可以使用Map来存放其中Key可以为用户标识
*/
private static CopyOnWriteArraySet<WebSocketController> webSocketSet = new CopyOnWriteArraySet<WebSocketController>(); private static CopyOnWriteArraySet<WebSocketController> webSocketSet = new CopyOnWriteArraySet<WebSocketController>();
/**
//与某个客户端的连接会话需要通过它来给客户端发送数据 * 与某个客户端的连接会话需要通过它来给客户端发送数据
*/
private Session session; private Session session;
//连接用户id //连接用户id
private String userId; private Long userId;
/** /**
* 连接建立成功调用的方法 * 连接建立成功调用的方法
* @param session 可选的参数session为与某个客户端的连接会话需要通过它来给客户端发送数据 * @param session 可选的参数session为与某个客户端的连接会话需要通过它来给客户端发送数据
*/ */
@OnOpen @OnOpen
public void onOpen(@PathParam("userId") String userId,Session session){ public void onOpen(@PathParam("userId") Long userId,Session session){
this.userId =userId; this.userId =userId;
this.session = session; this.session = session;
webSocketSet.add(this); //加入set中 webSocketSet.add(this); //加入set中
@ -80,6 +93,11 @@ public class WebSocketController {
" \"remainingTime\": \"45\"\n" + " \"remainingTime\": \"45\"\n" +
" }\n" + " }\n" +
"}"; "}";
ApplicationContext act = ApplicationContextRegister.getApplicationContext();
RedisService redisService = act.getBean(RedisService.class);
Object cacheObject = redisService.getCacheObject("gun:1472583698524602.seqdec");
System.out.println("cacheObject:"+cacheObject);
this.session.getBasicRemote().sendText(message); this.session.getBasicRemote().sendText(message);
} catch (IOException e) { } catch (IOException e) {
@ -98,16 +116,6 @@ public class WebSocketController {
error.printStackTrace(); 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() { public static synchronized int getOnlineCount() {
return onlineCount; return onlineCount;
} }

View File

@ -43,12 +43,10 @@ public class XhpcHistoryOrderController extends BaseController {
* 用户订单详情接口 * 用户订单详情接口
* @param userId 用户id * @param userId 用户id
* @param historyOrderId 历史订单id * @param historyOrderId 历史订单id
* @param type 1 历史订单id 2 充电订单id
* @param chargingOrderId 充电订单id
* @return * @return
*/ */
@GetMapping("/gethistotyOrderMessage") @GetMapping("/gethistotyOrderMessage")
public AjaxResult gethistotyOrderMessage(@RequestParam Long userId,@RequestParam Long historyOrderId,Integer type,Long chargingOrderId) public AjaxResult gethistotyOrderMessage(@RequestParam Long userId,@RequestParam Long historyOrderId)
{ {
return xhpcHistoryOrderService.gethistotyOrderMessage(userId,historyOrderId); return xhpcHistoryOrderService.gethistotyOrderMessage(userId,historyOrderId);
} }

View File

@ -0,0 +1,17 @@
package com.xhpc.order.controller;
import com.xhpc.common.core.web.controller.BaseController;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yuyang
* @date 2021/8/9 21:15
*/
@RestController
@RequestMapping("/realTimeOrder")
@Api(value = "实时订单接口", tags = "实时订单接口")
public class XhpcRealTimeOrderController extends BaseController {
}

View File

@ -50,8 +50,7 @@ public class HxpcChargeOrderServiceImpl implements IHxpcChargeOrderService {
@Override @Override
public AjaxResult getHistotyChargeOrderStatusList(Long userId) { public AjaxResult getHistotyChargeOrderStatusList(Long userId) {
return AjaxResult.success(hxpcChargeOrderMapper.getHistotyChargeOrderStatusList(userId));
return null;
} }
@Override @Override

View File

@ -206,7 +206,23 @@
</update> </update>
<select id="getHistotyChargeOrderStatusList" resultType="map"> <select id="getHistotyChargeOrderStatusList" resultType="map">
select
cor.charge_order_id as chargeOrderId,
cor.create_time as createTime,
cor.serial_number as serialNumber,
cs.name as chargingStationName,
ter.name as terminalName,
ho.history_order_id as historyOrderId,
ho.act_price as actPrice,
cor.charging_time as chargingTime,
cor.charging_degree as chargingDegree,
cor.status as status
from xhpc_charge_order as cor
left join xhpc_charging_station as cs on cs.charging_station_id = cor.charging_station_id
left join xhpc_terminal as ter on ter.terminal_id=cor.terminal_id
left join xhpc_history_order as ho on ho.charge_order_id = cor.charge_order_id
where cor.status =2 and cor.del_flag =0 and cor.user_id=#{userId}
order by cor.update_time desc
</select> </select>
</mapper> </mapper>

View File

@ -300,7 +300,7 @@
ho.history_order_id as historyOrderId, ho.history_order_id as historyOrderId,
ho.serial_number as serialNumber, ho.serial_number as serialNumber,
ho.create_time as createTime, ho.create_time as createTime,
ho.type as type, co.status as status,
cs.name as chargingStationName, cs.name as chargingStationName,
te.name as terminalName, te.name as terminalName,
ho.act_price as actPrice, ho.act_price as actPrice,