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 @@ 实时作弊信息 -
实时作弊信息
- +
+
+ 实时作弊信息
+
+