server.js 1.1 KB

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