Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var
- app = require('http').createServer(handler),
- io = require('socket.io')(app),
- //redis = require('redis'),
- fs = require('fs')
- //redisClient = redis.createClient();
- app.listen(3000);
- function handler (req, res) {
- console.log('1');
- fs.readFile(__dirname + '/index.html', function(err, data) {
- if(err) {
- console.log('3');
- res.writeHead(500);
- return res.end('Error loading index.html');
- }
- console.log('2');
- res.writeHead(200);
- res.end(data);
- });
- }
- /***
- Redis Channels Subscribes
- ***/
- //redisClient.subscribe('videocall');
- /***
- Redis Events
- ***/
- /*
- redisClient.on('message', function(channel, message) {
- console.log('CHANNEL: ' + channel);
- console.log('MESSAGE: ' + message);
- message = JSON.parse(message);
- console.log(message['doctor_user_id']);
- io.to(message['doctor_user_id']).emit(channel, message['quote_id']);
- io.to(message['patient_user_id']).emit(channel, message['quote_id']);
- });
- */
- /***
- Socket.io Connection Event
- ***/
- io.on('connection', function(socket) {
- socket.emit('welcome', { message: 'Esto es un mensaje que mando yos'} );
- /***
- Socket.io Events
- ***/
- room = "";
- socket.on('join', function(data) {
- console.log(io.sockets.adapter.rooms);
- socket.join(data);
- console.log(io.sockets.adapter.rooms);
- console.log('Join in server '+ data);
- socket.emit('joined', { message: 'Joined room: ' + data });
- });
- socket.on('changeIP', function(data) { //Se recibe un mensaje cuando el participante cuya ip es la de enlace refresca y cambia su ip
- console.log('ha cambiado la IP!');
- console.log('Enviamos a ' + data.channel + ' la nueva ip ' + data.ip);
- io.emit('newIp', data.ip);
- io.to(data.channel).emit('newIp', data.ip);
- });
- socket.on('leave', function(data) {
- room = data.room;
- console.log(io.sockets.adapter.rooms);
- socket.leave(data.room);
- console.log('2');
- console.log(io.sockets.adapter.rooms);
- console.log('Leave in server'+data.room);
- socket.emit('leaved', { message: 'Leaved room: ' + data.room });
- });
- socket.on('disconnected', function(data){
- console.log("Se ha ido el usuario "+data.user);
- io.to(data.room).emit('userDisconnected', data.user);
- abandonedUser(data.user, data.room);
- });
- socket.on('finished', function(data){
- console.log("ha finalizado el usuario "+data.user);
- io.to(data.room).emit('userFinished', data.user);
- });
- socket.on('updateMyInfoReto', function(data){
- console.log('reto:', data.room);
- io.to(data.room).emit('updateInfoReto', data);
- });
- socket.on('state_connected', function(data) {
- if(states != null && states[data['room']] != 1){
- states[data['room']] = 1;
- changeState(data['room'].substring(1, data['room'].length), 1, null);
- }
- });
- socket.on('state_reto', function(data) {
- if(states != null && states[data['room']] != 2) {
- states[data['room']] = 2;
- console.log('reto: ' + states[data['room']]);
- changeState(data['room'].substring(1, data['room'].length), 2, null);
- }
- });
- socket.on('state_test', function(data) {
- if(states != null && states[data['room']] != 3){
- states[data['room']] = 3;
- console.log('test: '+ states[data['room']]);
- changeState(data['room'].substring(1, data['room'].length), 3, data['test']);
- }
- });
- socket.on('state_examen', function(data) {
- if(states != null && states[data['room']] != 4) {
- states[data['room']] = 4;
- console.log('examen: ' + states[data['room']]);
- changeState(data['room'].substring(1, data['room'].length), 4, data['reto']);
- }
- });
- });
- function abandonedUser(user_id, idReto){
- //console.log(user_id);
- var query = connection.query('UPDATE alumnosRetos SET aceptado = ? WHERE alumnosRetos.idAlumno = ? and alumnosRetos.idReto = ?' , [3, user_id, idReto], function(error, result){
- console.log(result);
- }
- );
- }
- function changeState(user_id, state, idTestExamen){
- console.log(state);
- console.log(user_id);
- var query = connection.query('UPDATE alumnos SET estado = ?, idTestReto = ? WHERE alumnos.id = ?' , [state, idTestExamen,user_id], function(error, result){
- console.log(result);
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement