From f47a3bdbf7571ccf5b8d255bb425938235ecfad3 Mon Sep 17 00:00:00 2001 From: lichong <18518571399@163.com> Date: Tue, 9 Jul 2024 17:06:53 +0800 Subject: [PATCH] 12344 --- api/main.py | 114 +++++++++----------------------- front/package.json | 2 +- front/src/views/student.vue | 127 +++++++++++++++++++++++++----------- front/src/views/teacher.vue | 82 +++++++++++++++++++++-- test.py | 22 ------- 5 files changed, 198 insertions(+), 149 deletions(-) delete mode 100644 test.py diff --git a/api/main.py b/api/main.py index 983f853..f50ad25 100644 --- a/api/main.py +++ b/api/main.py @@ -4,7 +4,7 @@ from bson import ObjectId import hashlib import json from flask import Flask, render_template, request -from flask_socketio import SocketIO, Namespace, emit +from flask_socketio import SocketIO, emit, join_room, leave_room import time app = Flask(__name__) @@ -12,95 +12,46 @@ client = MongoClient("mongodb://localhost:27019/") db = client["back"] collection = db["users"] # sockitIo解决跨域问题 -socketMsg = SocketIO(app, cors_allowed_origins="*") -socketVedio = SocketIO(app, cors_allowed_origins="*") -# 用来存放客户端的 sid,即 session id -# 可以不单独定义字典存放 sid与namespace,flask-socketio 默认将 sid 存放在 room 中 -socket_pool = {} +app.config["SECRET_KEY"] = "secret!" +socketio = SocketIO(app, cors_allowed_origins="*") +# 存储连接的客户端的房间号 +clients = {} -# Websocket 通过namespace 和 sid 标识具体客户端 -# 第一个 Websocket 类用于聊天 -class MyCustomNamespace(Namespace): - name_space = "/msg" +@socketio.on("connect", namespace="/ws/video") +def on_connect(): + print("Client connected") - # 连接成功调用的方法 - def on_connect(self): - global socket_pool - socket_pool[request.sid] = self.name_space - print(1, socket_pool) - print(2, "Client connected") - print(3, "-----------------") - # 断开连接调用的方法 - def on_disconnect(self): - global socket_pool - print(4, "disconnect...") - del socket_pool[request.sid] - print(5, socket_pool) +@socketio.on("disconnect", namespace="/ws/video") +def on_disconnect(): + print("Client disconnected") + # 移除客户端的房间关联 + room = clients.pop(request.sid, None) + if room: + leave_room(room) - # 往 接收客户端标消息识为 'message' 的方法 - def on_message(self, data): - print(6, data) - # 把消息发送到客户端的 'response' 标识的方法中, 一般是 on_response() - emit("response", "123", to=request.sid) - print(7, "--------------") - print(8, request.sid) - print(9, "--------------") - print(10, socketio.server.manager.rooms) - # 往 接收客户端标消息识为 'hello' 的方法 - def on_hello(self, data): - print(11, "hello world") +@socketio.on("join", namespace="/ws/video") +def on_join(data): + room = data["room"] + join_room(room) + clients[request.sid] = room - # 发送消息 - def send(self, data): - # 向 namespace中的所有 Websocket 连接广播消息, namespace参数不能少, to缺省是广播模式 - emit("response", data, namespace=self.name_space) - # 向 sid 所标识的客户端 单播 - emit("response", data, namespace=self.name_space, to=request.sid) +@socketio.on("leave", namespace="/ws/video") +def on_leave(data): + room = data["room"] + leave_room(room) + clients.pop(request.sid, None) -# 为了详细展示不同类,这里就不做继承 -# 第二个 Websocket 类用于视频 -class MyCustomNamespace1(Namespace): - name_space = "/vedio" - def on_connect(self): - global socket_pool - - print(12, "connect..") - print(13, request.namespace) - socket_pool[request.sid] = self.name_space - print(14, socket_pool) - - def on_disconnect(self): - global socket_pool - print(15, "disconnect...") - del socket_pool[request.sid] - print(16, socket_pool) - - def on_message(self, data): - print(17, data) - emit("response", "123") - self.send("asd") - - # 发送消息 - def send(self, data): - emit("response", data, namespace=self.name_space) - - -socketMsg.on_namespace(MyCustomNamespace("/msg")) -socketVedio.on_namespace(MyCustomNamespace("/vedio")) - - -@app.route("/sendMsg") -def sendMsg(): - for sid, namespace_value in socket_pool.items(): - print(18, sid) - emit("response", "123456", namespace=namespace_value, to=sid) - print(19, "ok") - return "ok" +@socketio.on("message", namespace="/ws/video") +def handle_video_frame(message): + # 广播视频帧给房间内的所有客户端,除了发送该帧的客户端 + room = clients.get(request.sid) + if room: + emit("message", message, room=room, include_self=False) # md5加密 @@ -332,5 +283,4 @@ def updatePassword(): if __name__ == "__main__": initData() # app.run(debug=True) - socketMsg.run(app, debug=True) - # socketVedio.run(app, debug=True) + socketio.run(app, debug=True) diff --git a/front/package.json b/front/package.json index 0bd5bf3..92ec342 100644 --- a/front/package.json +++ b/front/package.json @@ -28,4 +28,4 @@ "@vitejs/plugin-vue": "^4.6.2", "vite": "^5.0.8" } -} +} \ No newline at end of file diff --git a/front/src/views/student.vue b/front/src/views/student.vue index 8e83b2a..69f6c63 100644 --- a/front/src/views/student.vue +++ b/front/src/views/student.vue @@ -34,21 +34,25 @@ 实时作弊信息 -

实时作弊信息

- +

+ + 实时作弊信息 + + 开始考试 +

- 视频 + - + diff --git a/front/src/views/teacher.vue b/front/src/views/teacher.vue index 495c521..86816c4 100644 --- a/front/src/views/teacher.vue +++ b/front/src/views/teacher.vue @@ -1,6 +1,6 @@ - + diff --git a/test.py b/test.py deleted file mode 100644 index fd3cb56..0000000 --- a/test.py +++ /dev/null @@ -1,22 +0,0 @@ -from flask import Flask, render_template -from flask_socketio import SocketIO, emit, Namespace -from threading import Thread, Event - -app = Flask(__name__) -app.config['SECRET_KEY'] = 'secret!' -socketio = SocketIO(app, cors_allowed_origins="*") - -class MyCustomNamespace(Namespace): - def on_connect(self): - print('Client connected') - - def on_disconnect(self): - print('Client disconnected') - - def on_message(self, message): - emit('response', {'data': 'Got it!'}, broadcast=True) - -socketio.on_namespace(MyCustomNamespace('/test')) - -if __name__ == '__main__': - socketio.run(app, debug=True) \ No newline at end of file