You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
825 B

7 years ago
7 years ago
7 years ago
  1. var express = require('express');
  2. var app = express();
  3. var http = require('http').Server(app);
  4. var io = require('socket.io')(http);
  5. app.use(express.static(__dirname + '/www'));
  6. //CORS
  7. app.use(function(req, res, next) {
  8. res.header("Access-Control-Allow-Origin", "*");
  9. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  10. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
  11. next();
  12. });
  13. io.set('origins', '*:*');
  14. io.on('connection', function(socket){
  15. console.log('a user connected');
  16. socket.on('msg', function (data, callback){
  17. console.log("msg");
  18. data.date= new Date();
  19. io.sockets.emit('newmsg', data); //aqí envia la data
  20. });
  21. });
  22. http.listen(3000, function(){
  23. console.log('listening on *:3000');
  24. });