Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. var
  2. app = require('http').createServer(handler),
  3. io = require('socket.io')(app),
  4. //redis = require('redis'),
  5. fs = require('fs')
  6. //redisClient = redis.createClient();
  7.  
  8. app.listen(3000);
  9.  
  10.  
  11. function handler (req, res) {
  12. console.log('1');
  13. fs.readFile(__dirname + '/index.html', function(err, data) {
  14. if(err) {
  15. console.log('3');
  16. res.writeHead(500);
  17. return res.end('Error loading index.html');
  18. }
  19. console.log('2');
  20. res.writeHead(200);
  21. res.end(data);
  22. });
  23. }
  24.  
  25. /***
  26. Redis Channels Subscribes
  27. ***/
  28. //redisClient.subscribe('videocall');
  29.  
  30. /***
  31. Redis Events
  32. ***/
  33. /*
  34. redisClient.on('message', function(channel, message) {
  35. console.log('CHANNEL: ' + channel);
  36. console.log('MESSAGE: ' + message);
  37. message = JSON.parse(message);
  38. console.log(message['doctor_user_id']);
  39. io.to(message['doctor_user_id']).emit(channel, message['quote_id']);
  40. io.to(message['patient_user_id']).emit(channel, message['quote_id']);
  41. });
  42. */
  43. /***
  44. Socket.io Connection Event
  45. ***/
  46. io.on('connection', function(socket) {
  47. socket.emit('welcome', { message: 'Esto es un mensaje que mando yos'} );
  48.  
  49. /***
  50. Socket.io Events
  51. ***/
  52. room = "";
  53. socket.on('join', function(data) {
  54. console.log(io.sockets.adapter.rooms);
  55. socket.join(data);
  56. console.log(io.sockets.adapter.rooms);
  57. console.log('Join in server '+ data);
  58. socket.emit('joined', { message: 'Joined room: ' + data });
  59. });
  60. socket.on('changeIP', function(data) { //Se recibe un mensaje cuando el participante cuya ip es la de enlace refresca y cambia su ip
  61. console.log('ha cambiado la IP!');
  62. console.log('Enviamos a ' + data.channel + ' la nueva ip ' + data.ip);
  63. io.emit('newIp', data.ip);
  64. io.to(data.channel).emit('newIp', data.ip);
  65. });
  66. socket.on('leave', function(data) {
  67. room = data.room;
  68. console.log(io.sockets.adapter.rooms);
  69. socket.leave(data.room);
  70. console.log('2');
  71. console.log(io.sockets.adapter.rooms);
  72. console.log('Leave in server'+data.room);
  73. socket.emit('leaved', { message: 'Leaved room: ' + data.room });
  74. });
  75. socket.on('disconnected', function(data){
  76. console.log("Se ha ido el usuario "+data.user);
  77. io.to(data.room).emit('userDisconnected', data.user);
  78. abandonedUser(data.user, data.room);
  79. });
  80. socket.on('finished', function(data){
  81. console.log("ha finalizado el usuario "+data.user);
  82. io.to(data.room).emit('userFinished', data.user);
  83. });
  84. socket.on('updateMyInfoReto', function(data){
  85. console.log('reto:', data.room);
  86. io.to(data.room).emit('updateInfoReto', data);
  87. });
  88. socket.on('state_connected', function(data) {
  89. if(states != null && states[data['room']] != 1){
  90. states[data['room']] = 1;
  91. changeState(data['room'].substring(1, data['room'].length), 1, null);
  92. }
  93. });
  94. socket.on('state_reto', function(data) {
  95. if(states != null && states[data['room']] != 2) {
  96. states[data['room']] = 2;
  97. console.log('reto: ' + states[data['room']]);
  98. changeState(data['room'].substring(1, data['room'].length), 2, null);
  99. }
  100. });
  101. socket.on('state_test', function(data) {
  102. if(states != null && states[data['room']] != 3){
  103. states[data['room']] = 3;
  104. console.log('test: '+ states[data['room']]);
  105. changeState(data['room'].substring(1, data['room'].length), 3, data['test']);
  106. }
  107. });
  108. socket.on('state_examen', function(data) {
  109. if(states != null && states[data['room']] != 4) {
  110. states[data['room']] = 4;
  111. console.log('examen: ' + states[data['room']]);
  112. changeState(data['room'].substring(1, data['room'].length), 4, data['reto']);
  113. }
  114. });
  115. });
  116.  
  117. function abandonedUser(user_id, idReto){
  118. //console.log(user_id);
  119. var query = connection.query('UPDATE alumnosRetos SET aceptado = ? WHERE alumnosRetos.idAlumno = ? and alumnosRetos.idReto = ?' , [3, user_id, idReto], function(error, result){
  120.  
  121. console.log(result);
  122.  
  123. }
  124. );
  125. }
  126.  
  127. function changeState(user_id, state, idTestExamen){
  128. console.log(state);
  129. console.log(user_id);
  130. var query = connection.query('UPDATE alumnos SET estado = ?, idTestReto = ? WHERE alumnos.id = ?' , [state, idTestExamen,user_id], function(error, result){
  131.  
  132. console.log(result);
  133.  
  134. }
  135. );
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement