server.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. });
  19. webRTC.rtc.on('send answer', function(rtc) {
  20. //answer sent
  21. });
  22. webRTC.rtc.on('disconnect', function(rtc) {
  23. //Client disconnect
  24. });
  25. webRTC.rtc.on('chat_msg', function(data, socket) {
  26. var roomList = webRTC.rtc.rooms[data.room] || [];
  27. for (var i = 0; i < roomList.length; i++) {
  28. var socketId = roomList[i];
  29. if (socketId !== socket.id) {
  30. var soc = webRTC.rtc.getSocket(data.room, socketId);
  31. if (soc) {
  32. soc.send(JSON.stringify({
  33. "eventName": "receive_chat_msg",
  34. "messages": data.messages,
  35. "color": data.color
  36. }), function(error) {
  37. if (error) {
  38. console.log(error);
  39. }
  40. });
  41. }
  42. }
  43. }
  44. });