package cn.com.goldenwater.dcproj.controller.system; import cn.com.goldenwater.core.web.BaseController; import cn.com.goldenwater.core.web.BaseResponse; import cn.com.goldenwater.dcproj.socket.WebSocketServer; import cn.com.goldenwater.id.util.UuidUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; /** *
* 利用webSocket实现实时打印的功能 *
* * @author luneyq * @author liyz * @date 2019/4/16 14:32 */ @Api(value = "webSocket相关接口", tags = "webSocket相关接口") @Controller @RequestMapping("/socket") public class WebSocketController extends BaseController { @ApiOperation("推送数据接口") @ResponseBody @RequestMapping("/push/{sid}") public String pushToWeb(@PathVariable String sid, String message) throws InterruptedException { for (int i = 0; i < 10; i++) { // sid为空时向所有连接广播群发 WebSocketServer.sendInfo(message + i, i == 5 ? null : sid); Thread.sleep(1000); } return "y"; } @ApiOperation("页面请求(返回ModelAndView)") @GetMapping("/view/{sid}") public ModelAndView socket(@PathVariable String sid) { ModelAndView mav = new ModelAndView("websocket"); mav.addObject("v1", "test1"); return mav; } @ApiOperation("页面请求(直接返回页面)") @RequestMapping(value = "/view2", method = RequestMethod.GET, produces = "text/html; charset=UTF-8") public String view() { return "websocket"; } @ApiOperation("获取UUID的接口") @RequestMapping(value = "/uuidStr", method = RequestMethod.GET) @ResponseBody public String getUuidStr() { return UuidUtil.uuid(); } @ApiOperation("获取UUID的接口") @RequestMapping(value = "/uuid", method = RequestMethod.GET) @ResponseBody public BaseResponse