首先源源不断的byte[]数组如何传给前端呢?
这里就可以用webSocket在回调函数中发送,这里C++的同事实现了startJniRecord这个native方法,这个回调函数会源源不断地发送数据给我,那么这里的执行顺序注意会先返回return CommonResult.success();再不停的循环回调函数里的内容。
@Operation(summary = "开始人脸录制")
@GetMapping("/startFaceRecording")
public CommonResult startFaceRecording() {
Boolean status = videoCapture.startJniRecord((byte[] frame, int width, int height) -> {
//将图像通过webSocket发送给前端
if (webSocketService.session != null) {
Map<String, Object> map = new HashMap<>();
map.put("imgBytes", Base64Utils.encode(frame));
map.put("width", width);
map.put("height", height);
String jsonStr = JSONUtil.toJsonStr(map);
WebSocketMessageUtil.sendWebSocketMessage(webSocketService.session, 200, jsonStr);
} else {
throw new RuntimeException("webSocket连接失败");
}
});
// Boolean status = videoCapture.startJniFaceRecording(null, (imageBytes, width, height) -> {
// //将图像通过webSocket发送给前端
// if (webSocketService.session != null) {
// Map<String, Object> map = new HashMap<>();
// map.put("imgBytes", Base64Utils.encode(imageBytes));
// map.put("width", width);
// map.put("height", height);
// String jsonStr = JSONUtil.toJsonStr(map);
// WebSocketMessageUtil.sendWebSocketMessage(webSocketService.session, 200, jsonStr);
// } else {
// throw new RuntimeException("webSocket连接失败");
// }
// });
if (!status) {
return CommonResult.error();
}
return CommonResult.success();
}
package com.wg.monitor.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.wg.monitor.model.WebSocketMessage;
import lombok.extern.slf4j.Slf4j;
import javax.websocket.Session;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class WebSocketMessageUtil {
/**
* 客户端的连接会话
*/
public static final ConcurrentHashMap<String, Session> SESSION_MAP = new ConcurrentHashMap<>();
/**
* @param session 客户端
* @param message 消息
* 向指定客户端推送消息
*/
public static void sendMessage(Session session, String message) {
if (ObjectUtil.isNotNull(session)) {
try {
synchronized (session){
session.getBasicRemote().sendText(message);
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
public static void close(Session session) {
log.info("连接关闭,sid=" + session.getId());
for (String key : SESSION_MAP.keySet()) {
if (SESSION_MAP.get(key).getId().equals(session.getId())) {
SESSION_MAP.remove(key);
}
}
}
/**
* 发送到所有用户
*/
public static void sendMessageToAll(String message) {
for (Session session : SESSION_MAP.values()) {
sendMessage(session, message);
}
}
/**
* @param session 客户端
* @param webSocketMessage 消息
* 向指定客户端推送WebSocketMessage消息
*/
public static void sendWebSocketMessage(Session session, WebSocketMessage webSocketMessage) {
String message = JSONUtil.toJsonStr(webSocketMessage);
sendMessage(session, message);
}
/**
* 从后端往前端发数据
* @param session
* @param code
* @param message
*/
public static void sendWebSocketMessage(Session session, Integer code, String message) {
WebSocketMessage webSocketMessage = new WebSocketMessage();
webSocketMessage.setCode(code);
webSocketMessage.setMsg(message);
sendWebSocketMessage(session, webSocketMessage);
}
/**
* 发送到所有用户
*/
public static void sendWebSocketMessageToAll(WebSocketMessage webSocketMessage) {
String message = JSONUtil.toJsonStr(webSocketMessage);
sendMessageToAll(message);
}
}
package com.wg.monitor.service;
import com.wg.monitor.util.WebSocketMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
/**
* ws://127.0.0.1:8085/api/monitor/websocket
*/
@Component
@Slf4j
@ServerEndpoint("/websocket")
public class WebSocketService {
public Session session;
@OnOpen
public void onOpen(Session session) throws IOException {
this.session = session;
log.info("客户端建立连接:{}", session);
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose(Session session) {
WebSocketMessageUtil.close(session);
}
/**
* 连接错误调用的方法
*/
@OnError
public void onError(Throwable error) {
log.error("错误原因:" + error.getMessage());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
**/
@OnMessage
public void onMessage(Session session, String message) {
log.info("收到并开始处理消息:{}", message);
}
}