Advertisement
bubblesupreme

balancer

Dec 7th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. const WebSocket = require('ws')
  2. const redis = require('redis')
  3. const response = require('../response.js');
  4. const redis_schema = require('../redis_schema.js');
  5.  
  6. clients = new Map()
  7.  
  8. const wss = new WebSocket.Server({
  9. host: "localhost",
  10. port: 3226,
  11. clientTracking: true
  12. });
  13.  
  14. const db_user = 1;
  15. var db = redis.createClient();
  16.  
  17. db.select(db_user, (err,res) => {
  18. console.log(`Switching to 1 db = ${res}`);
  19. });
  20.  
  21. db.on('connect', () => {
  22. console.log('Сonnected to Redis');
  23. });
  24.  
  25. const close_ws_connection = (ws)=> {
  26. redis_schema.set_user_properties(db, clients.get(ws));
  27. console.log(`Connection with user: ${clients.get(ws)} id has been closed`);
  28. }
  29.  
  30. const on_message = (ws, message) =>{
  31. try{
  32. var user = JSON.parse(message);
  33. if(user.user_id === undefined){
  34. let error = "Error! User_id has not been passed";
  35. console.log(error);
  36. let rsp = new response.OrdinaryResponse(true, error);
  37. ws.send(JSON.stringify(rsp));
  38. return;
  39. }
  40. else{
  41. console.log(`User has been connected with id ${user.user_id}`);
  42. clients.set(ws, user.user_id);
  43. redis_schema.set_user_properties(db, user.user_id, true, false);
  44. find_game(ws);
  45. return;
  46. }
  47. }
  48. catch(err){
  49. let error = "Invalid request";
  50. console.log(err);
  51. let rsp = new response.OrdinaryResponse(true, error);
  52. ws.send(JSON.stringify(rsp));
  53. return;
  54. }
  55. }
  56.  
  57. wss.on('connection', (ws) => {
  58. ws.on('close', () => {
  59. close_ws_connection(ws);
  60. })
  61.  
  62. ws.on('message', (message) => {
  63. on_message(ws, message);
  64. })
  65. })
  66.  
  67. const find_game = (ws) =>{
  68. console.log(clients.get(ws));
  69. const connection = new WebSocket("ws://localhost:3227");
  70.  
  71. connection.onclose = () => {
  72. let error = "Manager anavailable";
  73. let rsp = new response.OrdinaryResponse(false, error);
  74. for (let client of clients){
  75. client[0].send(JSON.stringify(rsp));
  76. }
  77. }
  78.  
  79. connection.onerror = () => {
  80. let error = "Manager anavailable";
  81. let rsp = new response.OrdinaryResponse(false, error);
  82. for (let client of clients){
  83. client[0].send(JSON.stringify(rsp));
  84. }
  85. }
  86. connection.onopen = () => {
  87. try{
  88. data = {
  89. "user_id":clients.get(ws)
  90. }
  91. connection.send(JSON.stringify(data));
  92. return;
  93. }
  94. catch(err){
  95. console.log(`Error ${err}`);
  96. return;
  97. }
  98. }
  99.  
  100. connection.onmessage = (e) => {
  101. data = JSON.parse(e.data);
  102. let current_client_id = clients.get(ws);
  103. if(data.pair_id !== undefined && data.pair_id !== current_client_id){
  104. for (let client of clients) {
  105. if(client[1] == data.pair_id){
  106. let game_info = new response.GameInfo(data.host, data.port, current_client_id);
  107. redis_schema.set_user_properties(db, data.pair_id, true, true);
  108. client[0].send(JSON.stringify(game_info));
  109. }
  110. }
  111. }
  112. ws.send(e.data);
  113. return;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement