diff --git a/app.js b/app.js index 6289326..c3be2ab 100644 --- a/app.js +++ b/app.js @@ -12,6 +12,7 @@ XXX: // 导入模块 var WebsocketServer = require('websocket').server; var http = require('http'); +const { connection } = require('websocket'); //const { client } = require('websocket'); // 创建http服务器(用于承载WS) @@ -19,7 +20,9 @@ var server = http.createServer(); // 端口设置端口 const PORT = 3000 || process.env.PORT // 客户端列表 -clientsList = []; +var clientsList = new Map(); +// 初始化public,这将是未指定房间默认加入的房间 +clientsList.set('public',[]); // http绑定端口 server.listen(PORT, () => { @@ -33,7 +36,7 @@ var wsServer = new WebsocketServer({httpServer:server}); wsServer.on('request', (websocketRequest) => { var connection = websocketRequest.accept(null, 'accepted-origin'); //将客户端插入终端列表 - clientsList.push(connection); + clientsList.get('public').push(connection); console.log('A client connected'); // 当收到消息时 @@ -54,14 +57,7 @@ wsServer.on('request', (websocketRequest) => { // 开发中,将客户端加入某个房间 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); + joinToRoom(cd.msg, connection) } // 房间内群发 @@ -75,11 +71,12 @@ wsServer.on('request', (websocketRequest) => { console.log('Unknown ERROR'); } - // 常规消息 - if(cd.opt == false){ - clientsList.forEach(element => { - element.sendUTF(cd.msg); - }); + // 常规消息将对所有 + if(cd.opt == null || cd.opt == false){ + // clientsList.forEach(element => { + // element.sendUTF(cd.msg); + // }); + broadCastForAll(cd.msg) } console.log(cd); } catch (error) { @@ -110,4 +107,58 @@ function BroadcastInRoom(roomid, tag, msg){ } }); console.log("CAST!!!!") +} + +function joinToRoom(roomid, connection){ + // 如果客户端对象没有设置过roomid属性则增设 + if(connection.roomid == undefined){ + connection.roomid = []; + //connection.roomid = cd.msg; + connection.roomid.push(roomid) + } + // 如果未创建过该房间,则创建 + if(!clientsList.has(roomid)){ + clientsList.set(roomid,[]); + console.log('Create Room: ' + roomid); + } + // 将client加入到指定roomid的list + clientsList.get(roomid).push(connection); + // 将传进来的roomid属性写入 + connection.roomid.push(cd.msg); + console.log("Set! roomid = " + connection.roomid); +} + +// 离开房间 +function leaveFromRoom(roomid, connection){ + // 如果客户端对象没有设置过roomid属性则返回错误信息 + if(connection.roomid == undefined){ + return false; + } + // 将client从ClientsList指定的房间roomid中移除并移入默认的public --可能会有问题 --Written By Copilot + clientsList.get(roomid).splice(clientsList.get(roomid).indexOf(connection), 1); + // 若房间内没有人了,则将房间移除 --可能会有问题 --Written By Copilot + if(clientsList.get(roomid).length == 0){ + clientsList.delete(roomid); + console.log('Delete Room: ' + roomid); + } + clientsList.get('public').push(connection); + + // 将client从指定的roomid中移除 -- 可能会有问题 -- Written By Copilot + connection.roomid.splice(connection.roomid.indexOf(roomid), 1); + console.log("Leave! roomid = " + connection.roomid); +} + + +// 获取已加入的房间 +function getJoinedRoom(connection){ + return connection.roomid +} + +// 广播消息 +function broadCastForAll(msg){ + clientsList.forEach(Listed => { + Listed.forEach(Client => { + Client.sendUTF(msg); + }); + }); } \ No newline at end of file diff --git a/web/index.html b/web/index.html index 4edd015..9c4db34 100644 --- a/web/index.html +++ b/web/index.html @@ -6,48 +6,8 @@
- +
+
注意:如果您加入房间后将自动退出public房间,您将不会收到公开消息,但是您可以重新加入到public房间以回复接收
+ \ No newline at end of file diff --git a/web/main.js b/web/main.js new file mode 100644 index 0000000..aa65a02 --- /dev/null +++ b/web/main.js @@ -0,0 +1,45 @@ +window.onload = function(){ + InitWS(); +} + +aROOM = 0 +function joinTheRoom(){ + 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': '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': {'roomid': aROOM}}; + ws.send(JSON.stringify(data)); +} + +function InitWS(){ + if("WebSocket" in window){ + console.log("您的浏览器支持Websocket"); + ws = new WebSocket("ws://localhost:3000"); + ws.onopen = function(){ + console.log("connected!"); + } + + ws.onmessage = function(evt){ + var rece = evt.data; + console.log("received: "+ rece); + } + + ws.onclose = function(){ + console.log("disconnected!"); + } + }else{ + alert("您的浏览器不支持Websocket"); + } +} \ No newline at end of file