server.js 1.3 KB

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