Advertisement
Guest User

test

a guest
Nov 15th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. /*
  2. // Minimal amount of secure websocket server
  3. var fs = require('fs');
  4.  
  5. // read ssl certificate
  6. var privateKey = fs.readFileSync('ssl-cert/privkey.pem', 'utf8');
  7. var certificate = fs.readFileSync('ssl-cert/fullchain.pem', 'utf8');
  8.  
  9. var credentials = { key: privateKey, cert: certificate };
  10. var https = require('https');
  11.  
  12. //pass in your credentials to create an https server
  13. var httpsServer = https.createServer(credentials);
  14. httpsServer.listen(8443);
  15.  
  16.  
  17.  
  18. var s = new WebSocketServer({
  19. server: httpsServer
  20. });
  21. */
  22.  
  23. /*function antidosmsg(lastmsg) {
  24. var msg = Date.now(); //time set
  25. var lastmsg = 0;
  26. var flag = 0;
  27. if ((msg-lastmsg) <= 300 && lastmsg != 0) { //if the time difference more than 300 and the last time is not 0, it will be executed.
  28. flag++; //set 1 flag
  29. }
  30. if ((msg-lastmsg >= 300) && flag != 0) { //if the time difference is less than 300 and a flag exists, it will be executed.
  31. flag--; //reset 1 flag
  32. }
  33. if (flag == 3) { //flag = 3 then disconnect client
  34. ws.close();
  35. }
  36. lastmsg = msg; //set last time
  37. return lastmsg
  38. }*/
  39.  
  40. var server = require('ws').Server;
  41. var s = new server({port: 5001});
  42. s.on('connection', function(ws) {
  43. if (typeof(lastmsg) == "undefined" && typeof(flag) == "undefined") {
  44. var lastmsg = 0;
  45. var flag = 0;
  46. }
  47. ws.on('message',function(message) {
  48. //no crash
  49. //massage = JSON.parse(message);
  50. //var lastmsg = antidosmsg(lastmsg);
  51.  
  52. var msg = Date.now(); //time set
  53.  
  54. if ((msg-lastmsg) <= 300 && lastmsg != 0) { //if the time difference more than 300 and the last time is not 0, it will be executed.
  55. flag++; //set 1 flag
  56. }
  57. if ((msg-lastmsg >= 300) && flag != 0) { //if the time difference is less than 300 and a flag exists, it will be executed.
  58. flag--; //reset 1 flag
  59. }
  60. if (flag == 3) { //flag = 3 then disconnect client
  61. ws.close();
  62. }
  63. lastmsg = msg; //set last time
  64.  
  65. console.log("Received: "+message);
  66. try {
  67. message = JSON.parse(message);
  68. } catch (e) {
  69. ws.send("Lade das benötigte Programm, um den Server zu kontaktieren.");
  70. return ;
  71. }
  72. if(message.type == "channel") {
  73. ws.channel = message.data;
  74. console.log("Channel: "+ws.channel);
  75. return;
  76. }
  77.  
  78. if(message.type == "password") {
  79. ws.password = message.data;
  80. console.log("Password: "+ws.password);
  81. return;
  82. }
  83.  
  84. if(message.type == "username") {
  85. ws.username = message.data;
  86. console.log("Username: "+ws.username);
  87. return;
  88. }
  89.  
  90. s.clients.forEach(function e(client) {
  91. //console.log();
  92. if (client.channel == ws.channel && client.password == ws.password && client != ws)
  93. //client.send(message);
  94. client.send(JSON.stringify({
  95. //name: ws.personName,
  96. //data: message.data,
  97. currentTime: message.data,
  98. Time: message.time,
  99. username: ws.username
  100. }));
  101. });
  102. });
  103.  
  104. ws.on('close', function(){
  105. console.log("i lost a client");
  106. });
  107. console.log("one more client connected");
  108. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement