Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 0.0.1

  • Loading branch information...
commit d6587cabcfe1d6983fa3d08292485d45bc976eb2 0 parents
@marcolanaro authored
22 .gitattributes
@@ -0,0 +1,22 @@
+# Auto detect text files and perform LF normalization
+* text=auto
+
+# Custom for Visual Studio
+*.cs diff=csharp
+*.sln merge=union
+*.csproj merge=union
+*.vbproj merge=union
+*.fsproj merge=union
+*.dbproj merge=union
+
+# Standard to msysgit
+*.doc diff=astextplain
+*.DOC diff=astextplain
+*.docx diff=astextplain
+*.DOCX diff=astextplain
+*.dot diff=astextplain
+*.DOT diff=astextplain
+*.pdf diff=astextplain
+*.PDF diff=astextplain
+*.rtf diff=astextplain
+*.RTF diff=astextplain
215 .gitignore
@@ -0,0 +1,215 @@
+#################
+## Eclipse
+#################
+
+*.pydevproject
+.project
+.metadata
+bin/
+tmp/
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.classpath
+.settings/
+.loadpath
+
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# CDT-specific
+.cproject
+
+# PDT-specific
+.buildpath
+
+
+#################
+## Visual Studio
+#################
+
+## Ignore Visual Studio temporary files, build results, and
+## files generated by popular Visual Studio add-ons.
+
+# User-specific files
+*.suo
+*.user
+*.sln.docstates
+
+# Build results
+
+[Dd]ebug/
+[Rr]elease/
+x64/
+build/
+[Bb]in/
+[Oo]bj/
+
+# MSTest test Results
+[Tt]est[Rr]esult*/
+[Bb]uild[Ll]og.*
+
+*_i.c
+*_p.c
+*.ilk
+*.meta
+*.obj
+*.pch
+*.pdb
+*.pgc
+*.pgd
+*.rsp
+*.sbr
+*.tlb
+*.tli
+*.tlh
+*.tmp
+*.tmp_proj
+*.log
+*.vspscc
+*.vssscc
+.builds
+*.pidb
+*.log
+*.scc
+
+# Visual C++ cache files
+ipch/
+*.aps
+*.ncb
+*.opensdf
+*.sdf
+*.cachefile
+
+# Visual Studio profiler
+*.psess
+*.vsp
+*.vspx
+
+# Guidance Automation Toolkit
+*.gpState
+
+# ReSharper is a .NET coding add-in
+_ReSharper*/
+*.[Rr]e[Ss]harper
+
+# TeamCity is a build add-in
+_TeamCity*
+
+# DotCover is a Code Coverage Tool
+*.dotCover
+
+# NCrunch
+*.ncrunch*
+.*crunch*.local.xml
+
+# Installshield output folder
+[Ee]xpress/
+
+# DocProject is a documentation generator add-in
+DocProject/buildhelp/
+DocProject/Help/*.HxT
+DocProject/Help/*.HxC
+DocProject/Help/*.hhc
+DocProject/Help/*.hhk
+DocProject/Help/*.hhp
+DocProject/Help/Html2
+DocProject/Help/html
+
+# Click-Once directory
+publish/
+
+# Publish Web Output
+*.Publish.xml
+*.pubxml
+
+# NuGet Packages Directory
+## TODO: If you have NuGet Package Restore enabled, uncomment the next line
+#packages/
+
+# Windows Azure Build Output
+csx
+*.build.csdef
+
+# Windows Store app package directory
+AppPackages/
+
+# Others
+sql/
+*.Cache
+ClientBin/
+[Ss]tyle[Cc]op.*
+~$*
+*~
+*.dbmdl
+*.[Pp]ublish.xml
+*.pfx
+*.publishsettings
+
+# RIA/Silverlight projects
+Generated_Code/
+
+# Backup & report files from converting an old project file to a newer
+# Visual Studio version. Backup files are not needed, because we have git ;-)
+_UpgradeReport_Files/
+Backup*/
+UpgradeLog*.XML
+UpgradeLog*.htm
+
+# SQL Server files
+App_Data/*.mdf
+App_Data/*.ldf
+
+#############
+## Windows detritus
+#############
+
+# Windows image file caches
+Thumbs.db
+ehthumbs.db
+
+# Folder config file
+Desktop.ini
+
+# Recycle Bin used on file shares
+$RECYCLE.BIN/
+
+# Mac crap
+.DS_Store
+
+
+#############
+## Python
+#############
+
+*.py[co]
+
+# Packages
+*.egg
+*.egg-info
+dist/
+build/
+eggs/
+parts/
+var/
+sdist/
+develop-eggs/
+.installed.cfg
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+
+#Translations
+*.mo
+
+#Mr Developer
+.mr.developer.cfg
77 Readme.md
@@ -0,0 +1,77 @@
+# DataChannel.IO
+
+Datachannel.io is inspired by the amazing socket.io framework and implements a real-time communication using the WebRTC technology.
+Peers are directly connected and datas are exchanged between clients without passing throug the server.
+
+Socket.io is only used to serve signals between clients. You can choose the namespace where socket.io server his signals.
+
+## Installing
+ npm install datachannel.io
+## Using with Node HTTP server
+#### On the Server
+ var app = require('http').createServer(handler)
+ , io = require('datachannel.io').listen(app, 'dataChannel');
+
+ app.listen(80);
+
+ function handler (req, res) {
+ fs.readFile(__dirname + '/index.html',
+ function (err, data) {
+ if (err) {
+ res.writeHead(500);
+ return res.end('Error loading index.html');
+ }
+ res.writeHead(200);
+ res.end(data);
+ });
+ }
+#### On the Client
+ var datachannel = new DataChannel({
+ socketServer: 'http://<yourIP>',
+ rtcServers: null,
+ nameSpace: 'dataChannel'
+ });
+## Using with the Express 3 web framework
+#### On the Server
+ var app = require('express')()
+ , server = require('http').createServer(app)
+ , dc = require('datachannel.io').listen(server, 'dataChannel');
+
+ app.listen(80);
+
+ function handler (req, res) {
+ fs.readFile(__dirname + '/index.html',
+ function (err, data) {
+ if (err) {
+ res.writeHead(500);
+ return res.end('Error loading index.html');
+ }
+ res.writeHead(200);
+ res.end(data);
+ });
+ }
+#### On the Client
+ var datachannel = new DataChannel({
+ socketServer: 'http://<yourIP>',
+ rtcServers: null,
+ nameSpace: 'dataChannel'
+ });
+## Join a Room (from the client)
+ datachannel.join("room");
+## Leave a Room (from the client)
+ datachannel.leave("room");
+## Send a Message
+ datachannel.in("room").emit("chat", {text: 'Hi!'});
+
+### ToDo
+
+- Test on Firefox
+- Implement e relay layer: in case peer-to-peer communication fails, socket.io serve the data message
+
+### Examples
+Some examples at:
+
+[https://github.com/marcolanaro/DataChannel.IO-Examples](https://github.com/marcolanaro/DataChannel.IO-Examples)
+
+
+Tested on Chrome Canary
197 client/datachannel.io.js
@@ -0,0 +1,197 @@
+var DataChannel = (function(window){
+
+ var User_Id;
+ var channels = [];
+ var socket;
+ var options;
+ var onCallbacks = [];
+ var rooms = [];
+
+
+ var Extend = function(destination, source){
+ for(var property in source)
+ if (source.hasOwnProperty(property)) {
+ if (typeof source[property]==="object") {
+ destination[property] = destination[property] || ((Object.prototype.toString.call(source[property])==="[object Array]") ? [] : {});
+ Extend(destination[property],source[property]);
+ } else
+ destination[property] = source[property];
+ }
+ return destination;
+ };
+
+ var _channel = {
+ onmessage: function(event) {
+ var message = JSON.parse(event.data);
+ for (var i = 0, l = onCallbacks[message.room][message.event].length; i < l; i += 1) {
+ onCallbacks[message.room][message.event][i](message.data);
+ }
+ },
+ onopen: function(event) {
+ var readyState = this.readyState;
+ },
+ onclose: function(event) {
+ var readyState = this.readyState;
+ }
+ };
+
+ function newChannel() {
+ return {
+ pc: new RTCPeerConnection(options.rtcServers, {optional: [{RtpDataChannels: true}]})
+ };
+ }
+
+ function setDescription(type, description, id) {
+ channels[id].pc.setLocalDescription(description);
+ socket.emit(type, { description: description, user_id: id });
+ }
+
+ function onIceCandidate(event, id) {
+ if (event.candidate) {
+ socket.emit('addIceCandidate', { candidate: event.candidate, user_id: id });
+ }
+ }
+
+ function receiveOffer(data) {
+ var id = data.user_id;
+
+ // Create Peer Connection
+ channels[id] = newChannel();
+
+ // Remote ICE candidate
+ channels[id].pc.onicecandidate = function(event) {
+ onIceCandidate(event, id);
+ };
+
+ // Receive Channel Callback
+ channels[id].pc.ondatachannel = function(event) {
+ Extend(event.channel, _channel);
+ channels[id].dc = event.channel;
+ };
+
+ channels[id].pc.setRemoteDescription(new RTCSessionDescription(data.description));
+
+ channels[id].pc.createAnswer(function(description) {
+ setDescription('answer', description, id);
+ });
+ }
+
+ function createConnection(id) {
+ // Create Peer Connection
+ channels[id] = newChannel();
+
+ try {
+ // Reliable Data Channels not yet supported in Chrome
+ // Data Channel api supported from Chrome M25.
+ // You need to start chrome with --enable-data-channels flag.
+ channels[id].dc = channels[id].pc.createDataChannel("sendDataChannel", {reliable: false});
+ } catch (e) {
+ }
+
+ channels[id].pc.onicecandidate = function(event) {
+ onIceCandidate(event, id);
+ };
+
+ Extend(channels[id].dc, _channel);
+
+ channels[id].pc.createOffer(function(description) {
+ setDescription('offer', description, id);
+ });
+ }
+
+ var socketInit = function(socketServer, nameSpace) {
+ socket = io.connect(socketServer + "/" + nameSpace);
+ socket.on('connected', function(data) {
+ User_Id = data.user_id;
+ });
+
+ socket.on('offer', function(data) {
+ receiveOffer(data);
+ });
+
+ socket.on('answer', function(data) {
+ channels[data.user_id].pc.setRemoteDescription(new RTCSessionDescription(data.description));
+ });
+
+ socket.on('addIceCandidate', function(data) {
+ channels[data.user_id].pc.addIceCandidate(new RTCIceCandidate(data.candidate));
+ });
+
+ socket.on('userJoined', function(data) {
+ if (!rooms[data.room])
+ rooms[data.room] = [];
+ if (rooms[data.room].indexOf(data.user_id) === -1)
+ rooms[data.room].push(data.user_id);
+ if (channels[data.user_id]) {
+
+ } else
+ createConnection(data.user_id);
+ });
+
+ socket.on('userLeaved', function(data) {
+ var idx = rooms[data.room].indexOf(data.user_id);
+ if (idx != -1) rooms[data.room].splice(idx, 1);
+ // if unused client, close RTC connection
+ var found = false;
+ for (var i = 0, l = rooms.length; i < l; i += 1) {
+ if (rooms[i].indexOf(data.user_id) !== -1)
+ found = true;
+ }
+ if (!found) {
+ channels[data.user_id].pc.close();
+ delete channels[data.user_id];
+ }
+ });
+
+ socket.on('usersInRoom', function(data) {
+ rooms[data.room] = data.users;
+ });
+ }
+
+ var C = function(o){
+ options = {
+ socketServer: o.socketServer || null,
+ rtcServers: o.rtcServers || null,
+ nameSpace: o.nameSpace || 'dataChannel'
+ };
+ socketInit(options.socketServer, options.nameSpace);
+ };
+
+ C.prototype = {
+ join: function(room) {
+ socket.emit('join', { room: room });
+ },
+ leave: function(room) {
+ socket.emit('leave', { room: room });
+ },
+ in: function(room) {
+ if (!rooms[room])
+ rooms[room] = [];
+ return {
+ emit: function(event, data) {
+ var message = JSON.stringify({
+ event: event,
+ room: room,
+ data: data
+ });
+ for (var i = 0, l = rooms[room].length; i < l; i += 1) {
+ var id = rooms[room][i];
+ if (channels[id])
+ channels[id].dc.send(message);
+ }
+ },
+ on: function(event, callback) {
+ if (!onCallbacks[room])
+ onCallbacks[room] = [];
+ if (!onCallbacks[room][event])
+ onCallbacks[room][event] = [];
+ onCallbacks[room][event].push(callback);
+ }
+ }
+ }
+ };
+
+
+ return C;
+
+}(window));
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/');
85 lib/index.js
@@ -0,0 +1,85 @@
+var io
+ , clients = []
+ , parse = require('url').parse
+ , send = require('send');
+
+var Server = function(srv, nameSpace) {
+ if (!(this instanceof Server)) return new Server(srv, nameSpace);
+ this._nameSpace = nameSpace || 'dataChannel';
+ this._path = '/datachannel.io';
+ this.serve(srv);
+ this.socketIO(srv);
+}
+
+Server.prototype.socketIO = function(srv) {
+ io = require('socket.io').listen(srv);
+ var namespace = io.of('/' + this._nameSpace);
+
+ namespace.on('connection', function (socket) {
+ var id = socket.id;
+ clients[id] = socket;
+
+ var myRoom = [];
+
+ socket.emit('connected', { user_id: id }); // aggiungere function come ack per il join
+
+ socket.on('offer', function (data) {
+ if (clients[data.user_id])
+ clients[data.user_id].emit('offer', { description: data.description, user_id: id });
+ });
+ socket.on('answer', function (data) {
+ if (clients[data.user_id])
+ clients[data.user_id].emit('answer', { description: data.description, user_id: id });
+ });
+ socket.on('addIceCandidate', function (data) {
+ if (clients[data.user_id])
+ clients[data.user_id].emit('addIceCandidate', { candidate: data.candidate, user_id: id });
+ });
+ socket.on('join', function (data) {
+ // se c'è già non la devo aggiungere
+ if (myRoom.indexOf(data.room) === -1)
+ myRoom.push(data.room);
+ socket.join(data.room);
+ namespace.in(data.room).emit('userJoined', { room: data.room, user_id: id});
+ var arr = namespace.clients(data.room);
+ var newArr = [];
+ for (var i = 0, l = arr.length; i < l; i += 1)
+ if (socket.id !== arr[i].id)
+ newArr.push(arr[i].id);
+ socket.emit('usersInRoom', { room: data.room, users: newArr });
+ });
+ socket.on('leave', function (data) {
+ var idx = myRoom.indexOf(data.room);
+ if (idx != -1) myRoom.splice(idx, 1);
+ socket.leave(data.room);
+ namespace.in(data.room).emit('userLeaved', { room: data.room, user_id: id});
+ });
+
+ socket.on('disconnect', function () {
+ delete clients[id];
+ for(var i = 0, l = myRoom.length; i < l; i += 1)
+ namespace.in(myRoom[i]).emit('userLeaved', { room: myRoom[i], user_id: id });
+ });
+ });
+}
+
+Server.prototype.serve = function(srv) {
+ var url = this._path + '/datachannel.io.js';
+ var evs = srv.listeners('request').slice(0);
+ srv.removeAllListeners('request');
+ srv.on('request', function(req, res) {
+ if (0 == req.url.indexOf(url)) {
+ var path = parse(req.url).pathname.split('/').slice(-1);
+ send(req, path)
+ .root(__dirname + '/../client')
+ .index(false)
+ .pipe(res);
+ } else {
+ for (var i = 0; i < evs.length; i++) {
+ evs[i].call(srv, req, res);
+ }
+ }
+ });
+};
+
+exports.listen = Server;
24 package.json
@@ -0,0 +1,24 @@
+{
+ "name": "datachannel.io",
+ "version": "0.0.1",
+ "description": "node.js realtime signaling server",
+ "keywords": [
+ "realtime",
+ "framework",
+ "websocket",
+ "webRTC",
+ "events",
+ "socket",
+ "io"
+ ],
+ "dependencies": {
+ "socket.io": "0.9.12",
+ "send": "~0.1.0"
+ },
+ "contributors": [
+ {
+ "name": "Marco Lanaro",
+ "email": "marcolanaro@gmail.com"
+ }
+ ]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.