123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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 deviceid: any = localStorage.getItem('deviceid');
- const myToken: any = 'JWT ' + token;
- let socketFace: any = null; //ws实例对象
- socketFace = io(address + '/', {
- transports: ['websocket', 'polling'],
- query: {
- type: 'face',
- Authorization: token ? myToken : ''
- }
- });
- function faceWs(callback: any) {
- if (socketFace == null) {
- return false;
- }
- callback({
- wksid: socketFace.id
- });
- socketFace.on('my_response', (e: any) => {
- callback(e);
- });
- socketFace.on('fe_device_init_result', (e: any) => {
- callback(e);
- });
- socketFace.on('facecontroller_ack', (e: any) => {
- callback(e);
- });
- socketFace.on('disconnect', (e: any) => {
- callback(e);
- });
- }
- /**
- * 发送消息
- */
- 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_ack',
- {
- cmd: 'check_facecontroller_available',
- ctrl_name: `facecontroller_${data}`
- },
- () => {}
- );
- }
- /**
- * 开启人脸识别模块
- */
- function openFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller_ack',
- {
- cmd: 'open_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => {}
- );
- }
- /**
- * 关闭人脸识别模块
- */
- function terminateFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller_ack',
- {
- cmd: 'terminate_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => {}
- );
- }
- /**
- * 暂停人脸识别模块
- */
- function suspendFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller_ack',
- {
- cmd: 'suspend_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => {}
- );
- }
- /**
- * 重启人脸识别模块
- */
- function resumeFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller_ack',
- {
- cmd: 'resume_facecontroller',
- ctrl_name: `facecontroller_${data}`
- },
- () => {}
- );
- }
- /**
- * 获取人脸识别模块状态
- */
- function getFace(data?: any, callback?: any) {
- sendMessage(
- 'facecontroller_ack',
- {
- cmd: 'get_facecontroller_state',
- ctrl_name: `facecontroller_${data}`
- },
- () => {}
- );
- }
- onBeforeUnmount(() => {
- if (socketFace) {
- socketFace.close();
- socketFace = null;
- }
- });
- return { faceWs,startDevice, sendMessage, checkFace, openFace, terminateFace, suspendFace, resumeFace, getFace };
- }
|