From 6c7e5fbc71a93d67004938769fc23c4f6a3f0623 Mon Sep 17 00:00:00 2001 From: Jerome Etienne Date: Mon, 18 Mar 2013 15:10:12 +0100 Subject: [PATCH] imported server --- .gitignore | 1 + index.html | 2 +- package.json | 33 ++++++++++++++++++++++++++++++++ server/server.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 package.json create mode 100644 server/server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/index.html b/index.html index 41131f8..31a9709 100644 --- a/index.html +++ b/index.html @@ -85,7 +85,7 @@ var room = 'public'; - var serverUrl = "ws://localhost:8080/"; + var serverUrl = "ws://localhost:8000/"; function onModelLoaded(){ // connect the server diff --git a/package.json b/package.json new file mode 100644 index 0000000..859a1a3 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "MultiWebRTC", + "description": "Example of multi user webrtc", + "version": "0.0.2-1", + "engines": { + "node": ">= 0.6.0" + }, + "dependencies": { + "webrtc.io": "latest", + "webrtc.io-client": "latest", + "express": "3.1.0", + "ws": "latest" + }, + "subdomain": "MultiWebRTC", + "scripts": { + "start": "example/server.js" + }, + "author": "Ben Brittain", + "contributors": [ + { + "name": "Ben Brittain", + "email": "ben@brittain.org" + }, + { + "name": "Dennis MÃ¥rtensson", + "email": "me@dennis.is" + }, + { + "name": "David Peter", + "email": "david.a.peter@gmail.com" + } + ] +} diff --git a/server/server.js b/server/server.js new file mode 100644 index 0000000..9d102f8 --- /dev/null +++ b/server/server.js @@ -0,0 +1,50 @@ +var express = require('express'); +var app = express() +var server = require('http').createServer(app); +var webRTC = require('webrtc.io').listen(server); + +var port = process.env.PORT || 8000; +server.listen(port); + + +// export static files +app.use('/', express.static(__dirname + '/../')); + + +webRTC.rtc.on('connect', function(rtc) { + //Client connected +}); + +webRTC.rtc.on('send answer', function(rtc) { + //answer sent +}); + +webRTC.rtc.on('disconnect', function(rtc) { + //Client disconnect +}); + +webRTC.rtc.on('chat_msg', function(data, socket) { + var roomList = webRTC.rtc.rooms[data.room] || []; + + for (var i = 0; i < roomList.length; i++) { + var socketId = roomList[i]; + + if (socketId !== socket.id) { + var soc = webRTC.rtc.getSocket(socketId); + + if (soc) { + soc.send(JSON.stringify({ + "eventName": "receive_chat_msg", + "data": { + "messages": data.messages, + "color": data.color + } + }), function(error) { + if (error) { + console.log(error); + } + }); + } + } + } +});