上课 socket

This commit is contained in:
zdg 2024-12-11 17:16:55 +08:00
parent bb0aa82e82
commit 13b1a895a4
9 changed files with 166 additions and 119 deletions

View File

@ -27,8 +27,9 @@ import msgUtils from '@/plugins/modal' // 消息工具
import * as API_entpcoursefile from '@/api/education/entpcoursefile' // api import * as API_entpcoursefile from '@/api/education/entpcoursefile' // api
import { PPTApi } from './api' import { PPTApi } from './api'
import { sessionStore } from '@/utils/store' // electron-store import { sessionStore } from '@/utils/store' // electron-store
import './api/watcher' // // import './api/watcher' //
import watcher from './api/watcher' //
watcher() //
const loading = ref(true) const loading = ref(true)
const _isPC = isPC() const _isPC = isPC()

View File

@ -14,6 +14,8 @@ const classcourse = sessionStore.get('curr.classcourse') // 课堂信息
export class Classcourse { export class Classcourse {
msgObj:ElMessageBox = null // 提示消息对象 msgObj:ElMessageBox = null // 提示消息对象
classcourse:any = null // 课堂信息
id: number|string = null // 课堂id
constructor() { constructor() {
this.load() this.load()
@ -22,6 +24,7 @@ export class Classcourse {
* @description * @description
*/ */
load() { load() {
console.log('classcourse-load', classcourse)
// 打开全屏 // 打开全屏
screenStore.setScreening(!!classcourse) screenStore.setScreening(!!classcourse)
// 如果课堂信息有值则连接socket // 如果课堂信息有值则连接socket
@ -30,13 +33,15 @@ export class Classcourse {
if (!ChatWs.ws) ChatWs.init() if (!ChatWs.ws) ChatWs.init()
ChatWs.id = classcourse.timgroupid // 群组id ChatWs.id = classcourse.timgroupid // 群组id
console.log('ws- ', classcourse) console.log('ws- ', classcourse)
classcourseStore.setClasscourse(classcourse) this.classcourse = classcourse // 课堂信息
this.id = classcourse.id // 课堂id
classcourseStore.setClasscourse(classcourse) // 课堂信息-状态管理
// 待上课提示 // 待上课提示
if (!classcourse.status) { if (!classcourse.status) {
this.msgObj = { this.msgObj = {
type: 'success', type: 'success',
title: '系统提示', title: '系统提示',
message: '公屏课堂已准备完毕,请等待老师开启课堂!', message: '公屏课堂已准备完毕,等待老师开启课堂...',
center: true, center: true,
showClose: false, showClose: false,
showCancelButton: false, showCancelButton: false,

View File

@ -120,6 +120,8 @@ export class MsgEnum {
MSG_classWorkOfPresentDataUpdate : 'classWorkOfPresentDataUpdate', MSG_classWorkOfPresentDataUpdate : 'classWorkOfPresentDataUpdate',
/** @desc: 课堂讲授活动,选择不同的内容 */ /** @desc: 课堂讲授活动,选择不同的内容 */
MSG_classlecturePagesrc : 'classlecturePagesrc', MSG_classlecturePagesrc : 'classlecturePagesrc',
/** @desc: 课堂作业|活动 */
MSG_homework : 'homework',
// === 新定义-消息头 === // === 新定义-消息头 ===
/** @desc: 课程创建-待开课 */ /** @desc: 课程创建-待开课 */
MSG_0000: 0x0000, MSG_0000: 0x0000,

View File

@ -10,39 +10,43 @@ import { MsgEnum } from './types' // 消息枚举
import ChatWs from '@/plugins/socket' // 聊天socket import ChatWs from '@/plugins/socket' // 聊天socket
import Classcourse from './classcourse' // 课程相关 import Classcourse from './classcourse' // 课程相关
import msgUtils from '@/plugins/modal' // 消息工具 import msgUtils from '@/plugins/modal' // 消息工具
const slidesStore = store.useSlidesStore() import { Homework } from './index' // api-作业相关
const classcourseStore = store.useClasscourseStore() // 课堂信息-状态管理
const resource = sessionStore.get('curr.resource') // apt 资源
const smarttalk = sessionStore.get('curr.smarttalk') // 备课资源
/** /**
* @description * @description
*/ */
// 监听幻灯片内容变化 export default () => {
watch(() => slidesStore.slides, (newVal, oldVal) => { const slidesStore = store.useSlidesStore()
const classcourseStore = store.useClasscourseStore() // 课堂信息-状态管理
const resource = sessionStore.get('curr.resource') // apt 资源
const smarttalk = sessionStore.get('curr.smarttalk') // 备课资源
window.test = slidesStore
// 监听幻灯片内容变化
watch(() => slidesStore.slides, (newVal, oldVal) => {
PPTApi.updateSlides(newVal, oldVal) // 更新幻灯片内容 PPTApi.updateSlides(newVal, oldVal) // 更新幻灯片内容
},{ deep: true }) },{ deep: true })
// 监听标题变化 // 监听标题变化
watch(() => slidesStore.title, (newVal, oldVal) => { watch(() => slidesStore.title, (newVal, oldVal) => {
if (oldVal == '未命名演示文稿') return // 初始加载,不需要更新数据 if (oldVal == '未命名演示文稿') return // 初始加载,不需要更新数据
updatePPT({title: newVal}) updatePPT({title: newVal})
}) })
// 消息监听ws // 消息监听ws
console.log('监听器已开启', ChatWs) // console.log('监听器已开启', ChatWs)
if (ChatWs.ws) { if (!!ChatWs.ws) {
ChatWs.watch((msg, e) => { ChatWs.watch((data, e) => {
try { try {
handleMessage(JSON.parse(msg)) handleMessage(JSON.parse(data)?.msg)
} catch (error) { } catch (error) {
console.error('socket 解析异常 ', error, e) console.error('socket 解析异常 ', error, e)
handleMessage(msg) handleMessage(data)
} }
}) })
} }
// 更新ppt内容 // 更新ppt内容
const updatePPT = async (data) => { const updatePPT = async (data) => {
if (!resource) return if (!resource) return
data.id = resource.id data.id = resource.id
await PPTApi.updateSlide(data) // 更新ppt内容 await PPTApi.updateSlide(data) // 更新ppt内容
@ -54,10 +58,11 @@ const updatePPT = async (data) => {
await PPTApi.updateSmarttalk(params) // 更新ppt内容 await PPTApi.updateSmarttalk(params) // 更新ppt内容
sessionStore.set('curr.smarttalk.fileShowName', params.fileShowName) sessionStore.set('curr.smarttalk.fileShowName', params.fileShowName)
} }
} }
// ws消息处理 // ws消息处理
const handleMessage = (msg) => { const handleMessage = (msg) => {
console.log('ws消息处理', msg)
if (typeof msg === 'object'){ if (typeof msg === 'object'){
const { head, content, ...other } = msg const { head, content, ...other } = msg
switch (head) { switch (head) {
@ -66,10 +71,11 @@ const handleMessage = (msg) => {
if (Classcourse.id !== content.id) { if (Classcourse.id !== content.id) {
msgUtils.alertError('老师开课信息异常,请重新进入公屏!') msgUtils.alertError('老师开课信息异常,请重新进入公屏!')
.then(() => { // 点击确定按钮,关闭窗口 .then(() => { // 点击确定按钮,关闭窗口
window.close() close()
}) })
} else { // 正常更新数据 } else { // 正常更新数据
classcourseStore.classcourse.status = 'open' classcourseStore.classcourse.status = 'open'
sessionStore.set('curr.classcourse.status', 'open')
// 更新课堂信息-关闭警告框 // 更新课堂信息-关闭警告框
Classcourse?.msgObj?.onVanish() Classcourse?.msgObj?.onVanish()
} }
@ -78,17 +84,29 @@ const handleMessage = (msg) => {
const slideIndex = content.current const slideIndex = content.current
slidesStore.updateSlideIndex(slideIndex) // 更新幻灯片下标 slidesStore.updateSlideIndex(slideIndex) // 更新幻灯片下标
break break
case MsgEnum.HEADS.MSG_homework: // 作业|活动-布置
if (!content.id) return
Homework.showHomework(content.id)
break
case MsgEnum.HEADS.MSG_closed: // 下课: case MsgEnum.HEADS.MSG_closed: // 下课:
window.close() // 关闭窗口 close()
break
case MsgEnum.HEADS.MSG_0001: // 点赞
break
case MsgEnum.HEADS.MSG_0002: // 疑惑
break
case MsgEnum.HEADS.MSG_0010: // 备用
break break
default: default:
break break
} }
} }
}
// 关闭窗口
const close = () => {
ChatWs?.close() // 关闭ws
setTimeout(() => {
window.close() // 关闭窗口
}, 1000)
}
} }
// console.log('监听器已开启', Classcourse)
// setTimeout(() => {
// console.log('关闭弹窗')
// // Classcourse.msgObj?.close()
// Classcourse?.msgObj?.onVanish()
// }, 10 * 1000)

View File

@ -1,5 +1,6 @@
import { useScreenStore, useSlidesStore, useClasscourseStore } from '../store' import { useScreenStore, useSlidesStore, useClasscourseStore } from '../store'
import { enterFullscreen, exitFullscreen, isFullscreen } from '../utils/fullscreen' import { enterFullscreen, exitFullscreen, isFullscreen } from '../utils/fullscreen'
import ChatWs from '@/plugins/socket' // 聊天socket
export default () => { export default () => {
const screenStore = useScreenStore() const screenStore = useScreenStore()
@ -23,7 +24,10 @@ export default () => {
const classcourse = classcourseStore.classcourse const classcourse = classcourseStore.classcourse
if (!!classcourse) { //DOTO 有课堂,执行退相关操作 if (!!classcourse) { //DOTO 有课堂,执行退相关操作
console.log('退出放映状态') console.log('退出放映状态')
window.close() ChatWs?.close() // 关闭ws
setTimeout(() => {
window.close() // 关闭窗口
}, 1000)
} else screenStore.setScreening(false) } else screenStore.setScreening(false)
if (isFullscreen()) exitFullscreen() if (isFullscreen()) exitFullscreen()
} }

View File

@ -94,6 +94,8 @@ export class MsgEnum {
MSG_classWorkOfPresentDataUpdate : 'classWorkOfPresentDataUpdate', MSG_classWorkOfPresentDataUpdate : 'classWorkOfPresentDataUpdate',
/** @desc: 课堂讲授活动,选择不同的内容 */ /** @desc: 课堂讲授活动,选择不同的内容 */
MSG_classlecturePagesrc : 'classlecturePagesrc', MSG_classlecturePagesrc : 'classlecturePagesrc',
/** @desc: 课堂作业|活动 */
MSG_homework : 'homework',
// === 新定义-消息头 === // === 新定义-消息头 ===
/** @desc: 课程创建-待开课 */ /** @desc: 课程创建-待开课 */
MSG_0000: 0x0000, MSG_0000: 0x0000,

View File

@ -28,6 +28,7 @@ export class ChatWs {
const userStore = useUserStore() // 用户信息 const userStore = useUserStore() // 用户信息
const wsBase = import.meta.env.VITE_APP_WS_URL; // ws地址 const wsBase = import.meta.env.VITE_APP_WS_URL; // ws地址
this.url = `${wsBase||ChatWs.base}/ws/websocket/${userStore.id}`; this.url = `${wsBase||ChatWs.base}/ws/websocket/${userStore.id}`;
this.closed = false; // 关闭状态 防止重连失败
// this.init(url); // this.init(url);
} }
ChatWs.instance = this; ChatWs.instance = this;
@ -38,6 +39,7 @@ export class ChatWs {
// 初始化 // 初始化
init(url) { init(url) {
!!url && (this.url = url); !!url && (this.url = url);
this.closed = false; // 关闭状态 防止重连失败
this.ws = null; this.ws = null;
const _this = this const _this = this
this.heartCheck = { this.heartCheck = {
@ -64,10 +66,11 @@ export class ChatWs {
}, this.timeout); }, this.timeout);
}, },
}; };
this.reconnect(); return this.reconnect();
} }
// 重连 // 重连
reconnect() { reconnect() {
return new Promise((resolve, reject) => {
const self = this; const self = this;
if (!!this.ws) { // 关闭之前的链接 if (!!this.ws) { // 关闭之前的链接
this.ws.close() this.ws.close()
@ -77,6 +80,7 @@ export class ChatWs {
this.ws.onopen = function () { this.ws.onopen = function () {
console.log("websocket-连接成功") console.log("websocket-连接成功")
self.heartCheck.reset().start(); self.heartCheck.reset().start();
resolve()
}; };
this.ws.onmessage = function (e) { this.ws.onmessage = function (e) {
// console.log("websocket-收到消息", e) // console.log("websocket-收到消息", e)
@ -97,10 +101,11 @@ export class ChatWs {
console.log("websocket-连接断开", e) console.log("websocket-连接断开", e)
self.connectSocket() // 重连 self.connectSocket() // 重连
}; };
})
} }
connectSocket() { connectSocket() {
this.heartCheck.reset() // 重置心跳 this.heartCheck.reset() // 重置心跳
if (self.closed) return; // 关闭状态不重连 if (this.closed) return this.ws = null; // 关闭状态不重连
// if(self.errCount <= 0) return; // 超过重连次数 // if(self.errCount <= 0) return; // 超过重连次数
// self.errCount--; // 重连次数减1 // self.errCount--; // 重连次数减1
if (this.errTime) { if (this.errTime) {
@ -157,6 +162,16 @@ export class ChatWs {
this.closed = true; this.closed = true;
this.ws.close(); this.ws.close();
} }
// 下课
closedCourse(id) {
return new Promise((resolve, reject) => {
this.sendMsg('closed', '下课', null, 'group', id)
resolve()
// setTimeout(() => {
// this.close() // 关闭链接
// }, 1000);
})
}
// 延时 ms 毫秒 // 延时 ms 毫秒
sleep(ms){ sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms)) return new Promise(resolve => setTimeout(resolve, ms))

View File

@ -14,9 +14,8 @@
<el-button size="small" type="primary" :icon="ChatDotRound" @click="chatSend()">上课(APP)</el-button> <el-button size="small" type="primary" :icon="ChatDotRound" @click="chatSend()">上课(APP)</el-button>
</template> </template>
<template v-else-if="item.status === 'open'"> <template v-else-if="item.status === 'open'">
<el-button :disabled="toolStore.isToolWin" size="small" type="primary" @click="startClassR(item)" <!-- <el-button :disabled="toolStore.isToolWin" size="small" type="primary" @click="startClassR(item)"
>继续上课</el-button >继续上课</el-button> -->
>
<!--<el-button v-if="item.status === '未开始'" @click="openEdit">编辑</el-button>--> <!--<el-button v-if="item.status === '未开始'" @click="openEdit">编辑</el-button>-->
<el-button :loading="loading" size="small" type="info" @click="endClassR(item)" <el-button :loading="loading" size="small" type="info" @click="endClassR(item)"
>下课{{ loading?'中...':'' }}</el-button> >下课{{ loading?'中...':'' }}</el-button>

View File

@ -380,7 +380,7 @@ export default {
} }
case 'close': { // case 'close': { //
const head = MsgEnum.HEADS.MSG_closed // closed const head = MsgEnum.HEADS.MSG_closed // closed
const msgT = MsgEnum.TYPES.TEACHER // teacher // const msgT = MsgEnum.TYPES.TEACHER // teacher
const isApt = !row.bookImg // bookImg ppt Apt const isApt = !row.bookImg // bookImg ppt Apt
row.ex3 == 'undefined' && (row.ex3 = null) row.ex3 == 'undefined' && (row.ex3 = null)
const timgroupid = isApt ? row.timgroupid : row.ex3 // ex3 ppt Apt const timgroupid = isApt ? row.timgroupid : row.ex3 // ex3 ppt Apt
@ -392,12 +392,13 @@ export default {
} }
// - // -
if (!!timgroupid) { if (!!timgroupid) {
const msg = { msgKey: head, actor: msgT, classcourseid: row.id } // const msg = { msgKey: head, actor: msgT, classcourseid: row.id }
Chat.sendMsg(timgroupid, msg) // Chat.sendMsg(timgroupid, msg)
await ChatWs.closedCourse(timgroupid)
} }
if (isApt) { // Apt if (isApt) { // Apt
// - // -
await updateClasscourse({ id: row.id, status: head, timgroupid: '' }) await updateClasscourse({ id: row.id, status: head })
} else { // PPT } else { // PPT
const toolStore = useToolState() const toolStore = useToolState()
// //
@ -410,12 +411,12 @@ export default {
} }
// //
setTimeout(async() => { setTimeout(async() => {
if (!!timgroupid) await Chat.dismissGroup(timgroupid) // if (!!timgroupid) await Chat.dismissGroup(timgroupid)
if (other.type == 1) { // - if (other.type == 1) { // -
other.instance.confirmButtonLoading = false other.instance.confirmButtonLoading = false
other.instance.confirmButtonText = '下课' other.instance.confirmButtonText = '下课'
other.done() other.done()
} else { } else { // -
other.loading.value = false other.loading.value = false
row.status = isApt ? head : '已结束' row.status = isApt ? head : '已结束'
} }