123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import io from 'socket.io-client';
- import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';
- export function useWebSocket() {
- const address: any = import.meta.env.VITE_APP_BASE_API;
- const token: any = localStorage.getItem('token');
- const myToken: any = 'JWT ' + token;
- let socketFace: any = null; //ws实例对象
- function faceWs(callback: any) {
- if (socketFace == null) {
- socketFace = io(address + '/', { transports: ['websocket', 'polling'], query: { type: 'face', Authorization: token ? myToken : '' } });
- }
- socketFace.on('my_response', (e: any) => {
- callback(e);
- });
- socketFace.on('fe_device_init_result', (e: any) => {
- e.type = "fe_device_init_result";
- if (e.device_info == undefined) {
- closeWS();
- }
- callback(e);
- });
- socketFace.on('facecontroller_ack', (e: any) => {
- callback(e);
- });
- socketFace.on('facecontroller_result', (e: any) => {
- e.type = "facecontroller_result";
- callback(e);
- });
- socketFace.on('disconnect', (e: any) => {
- let obj = { type: "disconnect" };
- callback(obj);
- });
- }
- /**
- * 发送消息
- */
- function sendMessage(type: string, data: any, callback?: () => void) {
- if (socketFace == null) {
- return false;
- }
- if (socketFace.connected) {
- callback = callback || function () { };
- socketFace.emit(type, data, callback);
- }
- }
- /**
- * 开始连接设备信息
- */
- function startDevice(data?: any, callback?: any) {
- sendMessage(
- 'fe_device_init',
- {
- data: data
- },
- () => { }
- );
- }
- /**
- * 查看人脸识别模块是否可用
- */
- function checkFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller',
- {
- cmd: 'check_facecontroller_available',
- ctrl_name: `facecontroller_${data}`
- },
- () => { }
- );
- }
- /**
- * 开启人脸识别模块
- */
- function openFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller',
- {
- cmd: 'open_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => { }
- );
- }
- /**
- * 关闭人脸识别模块
- */
- function terminateFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller',
- {
- cmd: 'terminate_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => {
- }
- );
- }
- /**
- * 暂停人脸识别模块
- */
- function suspendFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller',
- {
- cmd: 'suspend_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => { }
- );
- }
- /**
- * 重启人脸识别模块
- */
- function resumeFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller',
- {
- cmd: 'resume_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => { }
- );
- }
- /**
- * 获取人脸识别模块状态
- */
- function getFaceState(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller',
- {
- cmd: 'get_facecontroller_state',
- ctrl_name: `facecontroller_${data}`
- },
- () => { }
- );
- }
- /**
- * 关闭WS
- */
- function closeWS() {
- if (socketFace) {
- socketFace.close();
- socketFace = null;
- }
- }
- onBeforeUnmount(() => {
- closeWS();
- });
- return { faceWs, startDevice, sendMessage, checkFace, openFace, terminateFace, suspendFace, resumeFace, getFaceState, closeWS };
- }
|