|
|
|
@ -3,37 +3,36 @@ package com.xtong.zhbs.controller; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.xtong.zhbs.bean.PassengerFlow; |
|
|
|
import com.xtong.zhbs.service.PassengerFlowService; |
|
|
|
import com.xtong.zhbs.service.SeverSentEventService; |
|
|
|
import com.xtong.zhbs.utils.AjaxResult; |
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import io.swagger.annotations.ApiImplicitParam; |
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Controller; |
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
|
|
|
import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
import org.springframework.web.context.request.async.DeferredResult; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
|
* 实时客流 |
|
|
|
*/ |
|
|
|
@Api(tags = "客流趋势") |
|
|
|
@Controller |
|
|
|
@RestController |
|
|
|
@RequestMapping(value = "/real/") |
|
|
|
public class PassFlowController { |
|
|
|
@Resource |
|
|
|
private SeverSentEventService severSentEventService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private PassengerFlowService passengerFlowService; |
|
|
|
|
|
|
|
@ApiImplicitParam(name = "page",value = "姓名",required = true) |
|
|
|
@ApiOperation(value = "客流列表") |
|
|
|
@GetMapping(value = "/psflist") |
|
|
|
@ResponseBody |
|
|
|
public JSONObject psflist(@RequestParam String page, @RequestParam("limit") String limit){ |
|
|
|
int ipage = 1; |
|
|
|
int ilimit=10; |
|
|
|
@ -42,25 +41,43 @@ public class PassFlowController { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 数据流方式输出内容 |
|
|
|
* @param response |
|
|
|
* @return |
|
|
|
* @throws IOException |
|
|
|
* web连接的SSE目标地址, 保管凭证并返回这个会话资源给web,注意produces属性必须是文本事件流形式 |
|
|
|
* 凭证可以使用@PathVariable或者Url参数获取 |
|
|
|
* /approve/sse/message |
|
|
|
* @param token |
|
|
|
* @return SseEmitter |
|
|
|
* @author cloud9 |
|
|
|
* @date 2022/11/10 09:57 |
|
|
|
* |
|
|
|
*/ |
|
|
|
@ResponseBody |
|
|
|
@RequestMapping(value = "/getMsg", produces="text/event-stream;charset=UTF-8") |
|
|
|
DeferredResult<String> getMsg(HttpServletResponse response) throws IOException { |
|
|
|
response.setContentType("text/event-stream"); |
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
response.setStatus(200); |
|
|
|
List<PassengerFlow> passengerFlowList = passengerFlowService.getPassengerFlowList(1,10); |
|
|
|
// msgService.removeErrorResponse();
|
|
|
|
// msgService.getListRes().add(response);
|
|
|
|
if(!response.getWriter().checkError()){ |
|
|
|
response.getWriter().write("data:hello\n\n"); |
|
|
|
response.getWriter().flush(); |
|
|
|
} |
|
|
|
DeferredResult<String> df = new DeferredResult<String>(60000l); |
|
|
|
return df; |
|
|
|
@GetMapping(value = "/sse/message", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
|
|
|
public SseEmitter message(@RequestParam("clientToken") String token) { |
|
|
|
return severSentEventService.createConnection(token); |
|
|
|
} |
|
|
|
|
|
|
|
@GetMapping(value = "/boardcast") |
|
|
|
public JSONObject boardCast(){ |
|
|
|
String message="this is your Dad!!"; |
|
|
|
// SseEmitter.SseEventBuilder builder = SseEmitter
|
|
|
|
// .event()
|
|
|
|
// .data(message);
|
|
|
|
severSentEventService.sendMessageToClient(message); |
|
|
|
return AjaxResult.renderMsg("ookk"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 提供给POSTMAN测试调用,方便给web发送消息调试 |
|
|
|
* /approve/sse/message/send |
|
|
|
* @param map |
|
|
|
* @author cloud9 |
|
|
|
* @date 2022/11/10 10:25 |
|
|
|
* |
|
|
|
*/ |
|
|
|
@PostMapping("/sse/message/send") |
|
|
|
public void send(@RequestBody Map<String, String> map) { |
|
|
|
String client = map.get("client"); |
|
|
|
String txt = map.get("txt"); |
|
|
|
severSentEventService.sendMessageToClient(client, txt); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|