ocr识别-websocket
This commit is contained in:
@@ -141,7 +141,10 @@
|
||||
<artifactId>sevenzipjbinding</artifactId>
|
||||
<version>16.02-2.01</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
+8
-1
@@ -14,6 +14,7 @@ import com.jero.modules.ocr.util.UUIDUtils;
|
||||
import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.system.vo.OcrVO;
|
||||
import com.jero.modules.websocket.service.WebSocketServer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -58,6 +59,9 @@ public class OcrRestfulServiceImpl implements IOcrRestfulService {
|
||||
@Autowired
|
||||
private IOSSFileService ossFileService;
|
||||
|
||||
@Autowired
|
||||
private WebSocketServer webSocketServer;
|
||||
|
||||
|
||||
//请求处理文件url
|
||||
@Value("${OCR.handleFileUrl}")
|
||||
@@ -211,7 +215,10 @@ public class OcrRestfulServiceImpl implements IOcrRestfulService {
|
||||
ocrRecordEO.setJsonFileCode(null);
|
||||
ocrRecordEO.setUpdateTime(new Date());
|
||||
ocrRecordEO.setResultContent("转换成功");
|
||||
ocrRecordEOService.updateById(ocrRecordEO);
|
||||
boolean isSuccess = ocrRecordEOService.updateById(ocrRecordEO);
|
||||
if(isSuccess){
|
||||
webSocketServer.sendMessage();
|
||||
}
|
||||
|
||||
//记录doc文件信息
|
||||
OSSFile docFile = new OSSFile();
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.jero.modules.websocket.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
//@Configuration
|
||||
public class WebSocketConfig {
|
||||
|
||||
// @Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.jero.modules.websocket.service;
|
||||
|
||||
import javax.websocket.EndpointConfig;
|
||||
import javax.websocket.Session;
|
||||
|
||||
|
||||
public interface WebSocketServer {
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
* @param session session 对象
|
||||
*/
|
||||
public void onOpen(Session session, EndpointConfig config);
|
||||
|
||||
/**
|
||||
* 断开连接方法
|
||||
*/
|
||||
public void onClose(Session session);
|
||||
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
* @param session session 对象
|
||||
* @param message 返回客户端的消息
|
||||
*/
|
||||
public void onMessage(Session session, String message);
|
||||
|
||||
/**
|
||||
* 发生异常时触发的方法
|
||||
* @param session session 对象
|
||||
* @param throwable 抛出的异常
|
||||
*/
|
||||
public void onError(Session session,Throwable throwable);
|
||||
|
||||
/**
|
||||
* 向单个客户端发送消息
|
||||
*/
|
||||
public void sendMessage();
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.jero.modules.websocket.service.impl;
|
||||
|
||||
import com.jero.modules.websocket.service.WebSocketServer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
@Slf4j
|
||||
@ServerEndpoint(value = "/websocket/socketServer")
|
||||
@Component("webSocketService")
|
||||
public class WebsocketServerImpl implements WebSocketServer {
|
||||
|
||||
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
|
||||
private static CopyOnWriteArraySet<WebsocketServerImpl> webSocketSet = new CopyOnWriteArraySet<WebsocketServerImpl>();
|
||||
//这个session不是Httpsession,相当于用户的唯一标识,用它进行与指定用户通讯
|
||||
private Session session=null;
|
||||
|
||||
@OnOpen
|
||||
@Override
|
||||
public void onOpen(Session session, EndpointConfig config) {
|
||||
log.info("客户端 session id: "+session.getId()+ "打开一个连接");
|
||||
try {
|
||||
this.session = session;
|
||||
webSocketSet.add(this); //加入set中
|
||||
session.getBasicRemote().sendText("连接已建立");
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@OnClose
|
||||
@Override
|
||||
public void onClose(Session session) {
|
||||
// 客户端断开连接移除websocket对象
|
||||
webSocketSet.remove(session.getId());
|
||||
log.info("客户端断开连接,当前连接数:" + webSocketSet.size());
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
@Override
|
||||
public void onMessage(Session session, String message) {
|
||||
log.info("客户端 session id: "+session.getId()+",消息:" + message);
|
||||
|
||||
// 此方法为客户端给服务器发送消息后进行的处理,可以根据业务自己处理,这里返回页面
|
||||
// sendMessage(session, "服务端返回" + message);
|
||||
}
|
||||
|
||||
@OnError
|
||||
@Override
|
||||
public void onError(Session session, Throwable throwable) {
|
||||
log.error("发生错误"+ throwable.getMessage(),throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage() {
|
||||
//群发消息
|
||||
for(WebsocketServerImpl item: webSocketSet){
|
||||
try {
|
||||
item.session.getBasicRemote().sendText("1");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,9 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
@@ -44,4 +46,8 @@ public class LawNioApplication extends SpringBootServletInitializer {
|
||||
"----------------------------------------------------------");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user