< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-websocket</ artifactId> </ dependency>
@Configuration
public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter ( ) { return new ServerEndpointExporter ( ) ; }
}
webSocket拆帧进度推送
@Component
@ServerEndpoint ( "/ws/sp/{md5}" )
public class WebSocketSplitFrame { private final Logger logger = LoggerFactory . getLogger ( this . getClass ( ) ) ; private static final Map < String , Session > clients = new ConcurrentHashMap < > ( ) ; @OnOpen public void onOpen ( Session session, @PathParam ( "md5" ) String md5) { clients. put ( md5, session) ; logger. info ( "当前拆帧连接视频数量:{}" , clients. size ( ) ) ; } @OnMessage public int onMessage ( String md5AndNum) throws InterruptedException { String [ ] split = md5AndNum. split ( ":" , 2 ) ; String md5 = split[ 0 ] ; Integer num = Integer . valueOf ( split[ 1 ] ) ; File sfUiFile = FileUtil . getSfUiFile ( ) ; File splitFrameFile = new File ( sfUiFile. getPath ( ) + File . separator + md5) ; int length = 0 ; while ( num != length) { length = splitFrameFile. listFiles ( ) . length; Thread . sleep ( 400 ) ; sendFrameNum ( md5, length) ; } logger. info ( "素材已拆帧数完毕 md5:{} num:{}" , md5, length) ; return length; } public void sendFrameNum ( String md5, int num) { Session session = clients. get ( md5) ; if ( session != null ) { try { session. getBasicRemote ( ) . sendText ( num + "" ) ; } catch ( IOException e) { logger. error ( "sendFrameNum e:{}" , e. getMessage ( ) ) ; } } else { logger. info ( "[sendFrameNum]视频拆帧已完毕:{}" , md5) ; } } @OnClose public void onClose ( @PathParam ( "md5" ) String md5) { Session session = clients. get ( md5) ; StreamClose . close ( session) ; clients. remove ( md5) ; logger. info ( "[onClose]视频拆帧已完毕断开连接:{}" , md5) ; } }
WebSocket图片推送
@Component
@ServerEndpoint ( "/ws/fp/{md5}" )
public class WebSocketFramePic { private final Logger logger = LoggerFactory . getLogger ( this . getClass ( ) ) ; private final Map < String , Session > clients = new ConcurrentHashMap < > ( ) ; @OnOpen public void onOpen ( Session session, @PathParam ( "md5" ) String md5) { clients. put ( md5, session) ; logger. info ( "获取拆帧图连接数量:{}" , clients. size ( ) ) ; } @OnMessage public int onMessage ( String md5) throws InterruptedException { File splitFrameFile = new File ( FileUtil . getSfUiFile ( ) . getPath ( ) + File . separator + md5) ; LinkedList < File > mergePicFiles = Arrays . stream ( splitFrameFile. listFiles ( ) ) . sorted ( Comparator . comparingInt ( x -> Integer . parseInt ( x. getName ( ) . substring ( 0 , x. getName ( ) . lastIndexOf ( "." ) ) ) ) ) . collect ( Collectors . toCollection ( LinkedList :: new ) ) ; List < String > pics = new ArrayList < > ( ) ; for ( File mergePicFile : mergePicFiles) { byte [ ] bytes = FileUtil . getFileBytes ( mergePicFile) ; String base64str = Base64 . encodeBase64String ( bytes) ; String picBase64 = "data:image/jpeg;base64," + base64str; pics. add ( picBase64) ; if ( pics. size ( ) == 30 ) { Thread . sleep ( 1000 ) ; sendFramePic ( md5, pics) ; pics = new ArrayList < > ( ) ; } } logger. info ( "获取素材已拆帧图片 md5:{} num:{}" , md5, mergePicFiles. size ( ) ) ; return mergePicFiles. size ( ) ; } public void sendFramePic ( String md5, List < String > pics) { Session session = clients. get ( md5) ; if ( session != null ) { try { String text = JSONArray . toJSONString ( pics) ; session. getBasicRemote ( ) . sendText ( text) ; } catch ( Exception e) { logger. error ( "sendFramePic e:{}" , e. getMessage ( ) ) ; } } else { logger. info ( "[sendFrameNum]视频拆帧已完毕:{}" , md5) ; } } @OnClose public void onClose ( @PathParam ( "md5" ) String md5) { Session session = clients. get ( md5) ; StreamClose . close ( session) ; clients. remove ( md5) ; logger. info ( "[onClose]拆帧图片推送已完毕断开连接:{}" , md5) ; } }