app.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. TODO:
  3. * 在指定的房间内广播数据 BroadcastInRoom(roomid, msg)
  4. * 探索出能解决将同一roomid的客户加入房间的问题的方法
  5. FIXME:
  6. * 暂无待修正
  7. XXX:
  8. * 房间功能
  9. */
  10. // 导入模块
  11. var WebsocketServer = require('websocket').server;
  12. var http = require('http');
  13. //const { client } = require('websocket');
  14. // 创建http服务器(用于承载WS)
  15. var server = http.createServer();
  16. // 端口设置端口
  17. const PORT = 3000 || process.env.PORT
  18. // 客户端列表
  19. clientsList = [];
  20. // http绑定端口
  21. server.listen(PORT, () => {
  22. console.log("Server running on http://localhost:" + PORT);
  23. });
  24. // 在http服务器上运行WS服务器
  25. var wsServer = new WebsocketServer({httpServer:server});
  26. // 当客户端连入时
  27. wsServer.on('request', (websocketRequest) => {
  28. var connection = websocketRequest.accept(null, 'accepted-origin');
  29. //将客户端插入终端列表
  30. clientsList.push(connection);
  31. console.log('A client connected');
  32. // 当收到消息时
  33. connection.on('message', (msg) => {
  34. // 判断消息类型
  35. if(msg.type == 'utf8'){
  36. // 过滤非法数据
  37. try {
  38. /*
  39. 定义的数据传送格式规范(JSON):
  40. tag:数据标签
  41. msg:数据内容
  42. opt:Boolean,确认是否为执行操作,操作tag与用户传输的tag重合。
  43. args: opt参数
  44. */
  45. // 解析数据
  46. cd = JSON.parse(msg.utf8Data);
  47. // 开发中,将客户端加入某个房间
  48. if(cd.tag == 'setRoom' && cd.opt == true){
  49. // 如果客户端对象没有设置过roomid属性则增设
  50. if(connection.roomid == undefined){
  51. //connection.roomid = [];
  52. connection.roomid = cd.msg;
  53. }
  54. // 将传进来的roomid属性写入
  55. //connection.roomid.push(cd.msg);
  56. console.log("Set! roomid = " + connection.roomid);
  57. }
  58. // 房间内群发
  59. if(cd.tag == 'roomcast' && cd.opt == true && cd.arg != undefined){
  60. console.log('Cast!')
  61. BroadcastInRoom(cd.arg.roomid, 'cast', cd.msg);
  62. }else if(cd.opt == true && (cd.arg == undefined || cd.arg == "")){
  63. // 无参数提示
  64. console.log('Cannot GET arg');
  65. }else{
  66. console.log('Unknown ERROR');
  67. }
  68. // 常规消息
  69. if(cd.opt == false){
  70. clientsList.forEach(element => {
  71. element.sendUTF(cd.msg);
  72. });
  73. }
  74. console.log(cd);
  75. } catch (error) {
  76. console.log(error);
  77. console.error('Illegal Data');
  78. }
  79. }else{
  80. console.log(msg);
  81. }
  82. });
  83. connection.on('close', (reasonCode, description) => {
  84. console.log('A client disconnected');
  85. });
  86. })
  87. // 房间内消息广播
  88. function BroadcastInRoom(roomid, tag, msg){
  89. // for(var i = 0; i < clientsList.length; i++){
  90. // if(clientsList[i].roomid == roomid){
  91. // clientsList[i].send(JSON.stringify({'tag': tag, 'msg': msg}));
  92. // }
  93. // }
  94. clientsList.forEach(element => {
  95. if(element.roomid == roomid){
  96. element.send(JSON.stringify({'tag': tag, 'msg': msg}));
  97. }
  98. });
  99. console.log("CAST!!!!")
  100. }