From 5b6c5a85a341873eb751592c383f6359031b773f Mon Sep 17 00:00:00 2001
From: starlight <2682994272@qq.com>
Date: Thu, 22 Sep 2022 08:58:29 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=9B=AE=E5=BD=95=E7=BB=93?=
=?UTF-8?q?=E6=9E=84=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=BC=80=E5=8F=91Note?=
=?UTF-8?q?=E6=96=B9=E4=BE=BF=E5=AF=B9=E7=85=A7=E5=BC=80=E5=8F=91=E3=80=82?=
=?UTF-8?q?bugfix=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: Starlight-0208
---
DevelopNote.md | 19 +++++++
Development/README.md | 2 +
Development/app.js | 113 ++++++++++++++++++++++++++++++++++++++++++
app.js | 59 ++--------------------
web/index.html | 7 +--
5 files changed, 143 insertions(+), 57 deletions(-)
create mode 100644 DevelopNote.md
create mode 100644 Development/README.md
create mode 100644 Development/app.js
diff --git a/DevelopNote.md b/DevelopNote.md
new file mode 100644
index 0000000..e3081d4
--- /dev/null
+++ b/DevelopNote.md
@@ -0,0 +1,19 @@
+### 1. 关于数据的传送规范
+通用的数据传输规范
+```json
+{
+ 'tag': 'tag',
+ 'msg': 'message',
+ 'opt': True,
+ 'arg':{
+ 'arg1': value1,
+ 'arg2': value2,
+ ......
+ }
+}
+```
+> 解释:
+> 1. tag里一般存放的是操作标签或者房间名(这个可以自定义)。
+> 2. msg里存放的是消息,也可以是某个命令的主要参数,例如在加入房间的指令中,msg存放的是房间id。
+> 3. opt是一个Boolean类型的值,用来标识这个信息是否为指令,即用于操作WS服务器进行某些操作(例如加入房间,删除房间等等)。
+> 4. arg是一个obj,包括了这个命令的附加参数,在非命令的信息里最好还是不要存在arg,**后续可以考虑加入可以自定义的数据域,可以传输一些附加参数到服务器。**
\ No newline at end of file
diff --git a/Development/README.md b/Development/README.md
new file mode 100644
index 0000000..ae0ec84
--- /dev/null
+++ b/Development/README.md
@@ -0,0 +1,2 @@
+# ⚠ 这个文件夹用于开发
+这里边文件包含很多注释和未经测试的功能,不建议使用
\ No newline at end of file
diff --git a/Development/app.js b/Development/app.js
new file mode 100644
index 0000000..6289326
--- /dev/null
+++ b/Development/app.js
@@ -0,0 +1,113 @@
+/*
+TODO:
+* 在指定的房间内广播数据 BroadcastInRoom(roomid, msg)
+* 探索出能解决将同一roomid的客户加入房间的问题的方法
+FIXME:
+* 暂无待修正
+XXX:
+* 房间功能
+*/
+
+
+// 导入模块
+var WebsocketServer = require('websocket').server;
+var http = require('http');
+//const { client } = require('websocket');
+
+// 创建http服务器(用于承载WS)
+var server = http.createServer();
+// 端口设置端口
+const PORT = 3000 || process.env.PORT
+// 客户端列表
+clientsList = [];
+
+// http绑定端口
+server.listen(PORT, () => {
+ console.log("Server running on http://localhost:" + PORT);
+});
+
+// 在http服务器上运行WS服务器
+var wsServer = new WebsocketServer({httpServer:server});
+
+// 当客户端连入时
+wsServer.on('request', (websocketRequest) => {
+ var connection = websocketRequest.accept(null, 'accepted-origin');
+ //将客户端插入终端列表
+ clientsList.push(connection);
+ console.log('A client connected');
+
+ // 当收到消息时
+ connection.on('message', (msg) => {
+ // 判断消息类型
+ if(msg.type == 'utf8'){
+ // 过滤非法数据
+ try {
+ /*
+ 定义的数据传送格式规范(JSON):
+ tag:数据标签
+ msg:数据内容
+ opt:Boolean,确认是否为执行操作,操作tag与用户传输的tag重合。
+ args: opt参数
+ */
+ // 解析数据
+ cd = JSON.parse(msg.utf8Data);
+
+ // 开发中,将客户端加入某个房间
+ if(cd.tag == 'setRoom' && cd.opt == true){
+ // 如果客户端对象没有设置过roomid属性则增设
+ if(connection.roomid == undefined){
+ //connection.roomid = [];
+ connection.roomid = cd.msg;
+ }
+ // 将传进来的roomid属性写入
+ //connection.roomid.push(cd.msg);
+ console.log("Set! roomid = " + connection.roomid);
+ }
+
+ // 房间内群发
+ if(cd.tag == 'roomcast' && cd.opt == true && cd.arg != undefined){
+ console.log('Cast!')
+ BroadcastInRoom(cd.arg.roomid, 'cast', cd.msg);
+ }else if(cd.opt == true && (cd.arg == undefined || cd.arg == "")){
+ // 无参数提示
+ console.log('Cannot GET arg');
+ }else{
+ console.log('Unknown ERROR');
+ }
+
+ // 常规消息
+ if(cd.opt == false){
+ clientsList.forEach(element => {
+ element.sendUTF(cd.msg);
+ });
+ }
+ console.log(cd);
+ } catch (error) {
+ console.log(error);
+ console.error('Illegal Data');
+ }
+ }else{
+ console.log(msg);
+ }
+ });
+
+ connection.on('close', (reasonCode, description) => {
+ console.log('A client disconnected');
+ });
+
+})
+
+// 房间内消息广播
+function BroadcastInRoom(roomid, tag, msg){
+ // for(var i = 0; i < clientsList.length; i++){
+ // if(clientsList[i].roomid == roomid){
+ // clientsList[i].send(JSON.stringify({'tag': tag, 'msg': msg}));
+ // }
+ // }
+ clientsList.forEach(element => {
+ if(element.roomid == roomid){
+ element.send(JSON.stringify({'tag': tag, 'msg': msg}));
+ }
+ });
+ console.log("CAST!!!!")
+}
\ No newline at end of file
diff --git a/app.js b/app.js
index 6289326..ca6e34e 100644
--- a/app.js
+++ b/app.js
@@ -1,81 +1,38 @@
-/*
-TODO:
-* 在指定的房间内广播数据 BroadcastInRoom(roomid, msg)
-* 探索出能解决将同一roomid的客户加入房间的问题的方法
-FIXME:
-* 暂无待修正
-XXX:
-* 房间功能
-*/
-
-
-// 导入模块
var WebsocketServer = require('websocket').server;
var http = require('http');
-//const { client } = require('websocket');
-
-// 创建http服务器(用于承载WS)
var server = http.createServer();
-// 端口设置端口
const PORT = 3000 || process.env.PORT
-// 客户端列表
-clientsList = [];
-// http绑定端口
+clientsList = [];
server.listen(PORT, () => {
console.log("Server running on http://localhost:" + PORT);
});
-// 在http服务器上运行WS服务器
var wsServer = new WebsocketServer({httpServer:server});
-// 当客户端连入时
wsServer.on('request', (websocketRequest) => {
var connection = websocketRequest.accept(null, 'accepted-origin');
- //将客户端插入终端列表
clientsList.push(connection);
console.log('A client connected');
-
- // 当收到消息时
connection.on('message', (msg) => {
- // 判断消息类型
if(msg.type == 'utf8'){
- // 过滤非法数据
try {
- /*
- 定义的数据传送格式规范(JSON):
- tag:数据标签
- msg:数据内容
- opt:Boolean,确认是否为执行操作,操作tag与用户传输的tag重合。
- args: opt参数
- */
- // 解析数据
cd = JSON.parse(msg.utf8Data);
-
- // 开发中,将客户端加入某个房间
- if(cd.tag == 'setRoom' && cd.opt == true){
- // 如果客户端对象没有设置过roomid属性则增设
+ if(cd.tag == 'setRoom' && cd.opt == true){
if(connection.roomid == undefined){
- //connection.roomid = [];
connection.roomid = cd.msg;
}
- // 将传进来的roomid属性写入
- //connection.roomid.push(cd.msg);
console.log("Set! roomid = " + connection.roomid);
}
-
- // 房间内群发
if(cd.tag == 'roomcast' && cd.opt == true && cd.arg != undefined){
console.log('Cast!')
BroadcastInRoom(cd.arg.roomid, 'cast', cd.msg);
- }else if(cd.opt == true && (cd.arg == undefined || cd.arg == "")){
- // 无参数提示
+ }else if(cd.opt == true && (cd.arg == undefined || cd.arg == "")){
console.log('Cannot GET arg');
}else{
console.log('Unknown ERROR');
}
-
- // 常规消息
+
if(cd.opt == false){
clientsList.forEach(element => {
element.sendUTF(cd.msg);
@@ -94,16 +51,10 @@ wsServer.on('request', (websocketRequest) => {
connection.on('close', (reasonCode, description) => {
console.log('A client disconnected');
});
-
})
-// 房间内消息广播
+
function BroadcastInRoom(roomid, tag, msg){
- // for(var i = 0; i < clientsList.length; i++){
- // if(clientsList[i].roomid == roomid){
- // clientsList[i].send(JSON.stringify({'tag': tag, 'msg': msg}));
- // }
- // }
clientsList.forEach(element => {
if(element.roomid == roomid){
element.send(JSON.stringify({'tag': tag, 'msg': msg}));
diff --git a/web/index.html b/web/index.html
index 4edd015..b5aa991 100644
--- a/web/index.html
+++ b/web/index.html
@@ -3,9 +3,10 @@