server.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var app = require('express').createServer();
  2. var webRTC = require('webrtc.io').listen(8001);
  3. //When connectiong to nodejitsu
  4. //app.listen(80);
  5. //When using localhost
  6. app.listen(8000);
  7. app.get('/', function(req, res) {
  8. res.sendfile(__dirname + '/index.html');
  9. });
  10. app.get('/style.css', function(req, res) {
  11. res.sendfile(__dirname + '/style.css');
  12. });
  13. app.get('/webrtc.io.js', function(req, res) {
  14. res.sendfile(__dirname + '/webrtc.io.js');
  15. });
  16. webRTC.rtc.on('connect', function(rtc) {
  17. //Client connected
  18. console.log('connection');
  19. rtc.on('send answer', function() {
  20. //answer sent
  21. console.log('send answer');
  22. });
  23. rtc.on('disconnect', function() {
  24. console.log('disconnect');
  25. });
  26. rtc.on('chat_msg', function(data, socket) {
  27. var roomList = rtc.rooms[data.room] || [];
  28. console.log(roomList);
  29. for (var i = 0; i<roomList.length; i++) {
  30. var socketId = roomList[i];
  31. console.log(socketId);
  32. if (socketId == socket.id) {
  33. continue;
  34. }
  35. else {
  36. var soc = rtc.getSocket(data.room, socketId);
  37. if (soc) {
  38. console.log('chat_msg send');
  39. soc.send(JSON.stringify({
  40. "eventName": "receive_chat_msg",
  41. "messages": data.messages,
  42. "color": data.color
  43. }), function(error) {
  44. if (error) {
  45. console.log(error);
  46. }
  47. });
  48. }
  49. }
  50. }
  51. });
  52. });