优化目录结构,增加开发Note方便对照开发。bugfix。
Co-authored-by: Starlight-0208
This commit is contained in:
parent
f03892cf96
commit
5b6c5a85a3
19
DevelopNote.md
Normal file
19
DevelopNote.md
Normal file
@ -0,0 +1,19 @@
|
||||
### 1. 关于数据的传送规范
|
||||
通用的数据传输规范
|
||||
```json
|
||||
{
|
||||
'tag': 'tag',
|
||||
'msg': 'message',
|
||||
'opt': True,
|
||||
'arg':{
|
||||
'arg1': value1,
|
||||
'arg2': value2,
|
||||
......
|
||||
}
|
||||
}
|
||||
```
|
||||
> 解释:<br />
|
||||
> 1. tag里一般存放的是操作标签或者房间名(这个可以自定义)。<br />
|
||||
> 2. msg里存放的是消息,也可以是某个命令的主要参数,例如在加入房间的指令中,msg存放的是房间id。<br />
|
||||
> 3. opt是一个Boolean类型的值,用来标识这个信息是否为指令,即用于操作WS服务器进行某些操作(例如加入房间,删除房间等等)。<br />
|
||||
> 4. arg是一个obj,包括了这个命令的附加参数,在非命令的信息里最好还是不要存在arg,**后续可以考虑加入可以自定义的数据域,可以传输一些附加参数到服务器。**<br />
|
||||
2
Development/README.md
Normal file
2
Development/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# ⚠ 这个文件夹用于开发
|
||||
这里边文件包含很多注释和未经测试的功能,不建议使用
|
||||
113
Development/app.js
Normal file
113
Development/app.js
Normal file
@ -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!!!!")
|
||||
}
|
||||
59
app.js
59
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}));
|
||||
|
||||
@ -3,9 +3,10 @@
|
||||
<head>
|
||||
<title>Socket</title>
|
||||
</head>
|
||||
<body onload="InitWS()">
|
||||
<body">
|
||||
<div><input type="text" id="textin"><button onclick="send()">Submit</button> <button onclick="Broadcast()">Submit to room</button></div>
|
||||
<input type="text" id="roomid"><button onclick="joinTheRoom()">JOIN</button>
|
||||
<input type="text" id="roomid"><button onclick="joinTheRoom()">JOIN</button><br />
|
||||
<input type="text" id="WS-SERVER" value="ws://localhost:3000"><button onclick="InitWS()">Connect</button>
|
||||
<script>
|
||||
aROOM = 0
|
||||
function joinTheRoom(){
|
||||
@ -31,7 +32,7 @@
|
||||
function InitWS(){
|
||||
if("WebSocket" in window){
|
||||
console.log("您的浏览器支持Websocket");
|
||||
ws = new WebSocket("ws://localhost:3000");
|
||||
ws = new WebSocket(document.getElementById('WS-SERVER').value);
|
||||
ws.onopen = function(){
|
||||
console.log("connected!");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user