update
This commit is contained in:
21
README.md
Normal file
21
README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# WebSocket Server Version 2.0
|
||||
---
|
||||
## 1.概述
|
||||
经过了部分代码的重新编写,对其新增加了一部分功能。
|
||||
|
||||
## 2.新功能
|
||||
1. 房间
|
||||
2. 房间内群发
|
||||
|
||||
## 3.部署
|
||||
很简单!
|
||||
首先clone本存储库
|
||||
```
|
||||
git clone xxx
|
||||
```
|
||||
进入存储库后安装相关包
|
||||
```shell
|
||||
cd xxx
|
||||
npm install
|
||||
```
|
||||
自动执行完毕之后直接在shell中运行`node app.js`即可使用
|
||||
29
app.js
29
app.js
@@ -56,22 +56,31 @@ wsServer.on('request', (websocketRequest) => {
|
||||
if(cd.tag == 'setRoom' && cd.opt == true){
|
||||
// 如果客户端对象没有设置过roomid属性则增设
|
||||
if(connection.roomid == undefined){
|
||||
connection.roomid = [];
|
||||
//connection.roomid = [];
|
||||
connection.roomid = cd.msg;
|
||||
}
|
||||
// 将传进来的roomid属性写入
|
||||
connection.roomid.push(cd.msg);
|
||||
//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){
|
||||
}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);
|
||||
@@ -90,9 +99,15 @@ wsServer.on('request', (websocketRequest) => {
|
||||
|
||||
// 房间内消息广播
|
||||
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}));
|
||||
// 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!!!!")
|
||||
}
|
||||
@@ -12,18 +12,19 @@
|
||||
aROOM = document.getElementById('roomid').value;
|
||||
var Datas = {'tag': 'setRoom', 'msg': aROOM, 'opt': true};
|
||||
ws.send(JSON.stringify(Datas));
|
||||
console.log("Join to room " + aROOM);
|
||||
// 结果返回
|
||||
}
|
||||
|
||||
function send(){
|
||||
var context = document.getElementById('textin').value;
|
||||
var data = {'tag': 'roomcast', 'msg': context, 'opt': false};
|
||||
var data = {'tag': 'public', 'msg': context, 'opt': false};
|
||||
ws.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
function Broadcast(){
|
||||
var context = document.getElementById('textin').value;
|
||||
var data = {'tag': 'roomcast', 'msg': context, 'opt': true, 'arg': aROOM};
|
||||
var data = {'tag': 'roomcast', 'msg': context, 'opt': true, 'arg': {'roomid': aROOM}};
|
||||
ws.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user