server.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(socketId);
  28. if (soc) {
  29. soc.send(JSON.stringify({
  30. "eventName": "receive_chat_msg",
  31. "data": {
  32. "messages": data.messages,
  33. "color": data.color
  34. }
  35. }), function(error) {
  36. if (error) {
  37. console.log(error);
  38. }
  39. });
  40. }
  41. }
  42. }
  43. });