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 @@ Socket - +
- +
+