index.js 686 B

1234567891011121314151617181920212223242526272829
  1. var app = require('express')();
  2. var http = require('http').Server(app);
  3. var io = require('socket.io')(http);
  4. const cors = require('cors')
  5. app.use(cors())
  6. app.get('/', function(req, res){
  7. res.sendFile(__dirname + '/index.html');
  8. });
  9. io.on('connection', function(socket){
  10. console.log('an user connected with id', socket.id);
  11. socket.on('msg', function(msg){
  12. if (msg && msg.message && msg.nick){
  13. io.emit('msg', msg)
  14. }
  15. else {
  16. console.log('WRONG MESSAGE', msg)
  17. }
  18. })
  19. socket.on('disconnect', function(){
  20. console.log('user disconnected '+ socket.id);
  21. });
  22. });
  23. http.listen(4000, function(){
  24. console.log('listening on *:4000');
  25. });