Use the Twilio STUN/TURN service.

pull/18/head
Alex Kern 10 years ago
parent 5b2e85809f
commit 594fd54823

@ -0,0 +1,34 @@
var twilio = require('twilio')
var winston = require('winston')
if (process.env.TWILIO_SID && process.env.TWILIO_TOKEN) {
var client = twilio(process.env.TWILIO_SID, process.env.TWILIO_TOKEN)
} else {
var client = null
}
var CACHE_LIFETIME = 5 * 60 * 1000 // 5 minutes
var cachedPromise = null
function clearCache() {
cachedPromise = null
}
exports.getICEServers = function () {
if (client == null) return Promise.resolve({})
if (cachedPromise) return cachedPromise
cachedPromise = new Promise(function (resolve, reject) {
client.tokens.create({}, function(err, token) {
if (err) {
winston.error(err.message)
return resolve({})
}
setTimeout(clearCache, CACHE_LIFETIME)
resolve(token.ice_servers)
})
})
return cachedPromise
}

@ -1,11 +1,12 @@
var db = require('./db')
var express = require('express')
var expressWinston = require('express-winston')
var fs = require('fs')
var http = require('http')
var ice = require('./ice')
var path = require('path')
var socketIO = require('socket.io')
var winston = require('winston')
var expressWinston = require('express-winston')
var app = express()
var server = http.Server(app)
@ -62,6 +63,12 @@ io.on('connection', function (socket) {
})
})
socket.on('rtcConfig', function (_, res) {
ice.getICEServers().then(function (iceServers) {
res({ iceServers: iceServers })
})
})
socket.on('disconnect', function () {
db.remove(upload)
})

@ -1,7 +1,7 @@
import DownloadActions from '../actions/DownloadActions'
import webtorrent from '../webtorrent'
import alt from '../alt'
import socket from 'filepizza-socket'
import { getClient } from '../wt'
const SPEED_REFRESH_TIME = 2000
@ -34,7 +34,8 @@ export default alt.createStore(class DownloadStore {
if (this.status !== 'ready') return
this.status = 'requesting'
webtorrent.download(this.infoHash, (torrent) => {
getClient().then((client) => {
client.download(this.infoHash, (torrent) => {
this.setState({ status: 'downloading' })
const updateSpeed = () => {
@ -66,6 +67,7 @@ export default alt.createStore(class DownloadStore {
})
})
})
}
}, 'DownloadStore')

@ -1,7 +1,7 @@
import UploadActions from '../actions/UploadActions'
import alt from '../alt'
import socket from 'filepizza-socket'
import webtorrent from '../webtorrent'
import { getClient } from '../wt'
const SPEED_REFRESH_TIME = 2000
@ -24,7 +24,8 @@ export default alt.createStore(class UploadStore {
if (this.status !== 'ready') return
this.status = 'processing'
webtorrent.seed(file, (torrent) => {
getClient().then((client) => {
client.seed(file, (torrent) => {
const updateSpeed = () => {
this.setState({
@ -54,6 +55,7 @@ export default alt.createStore(class UploadStore {
})
})
})
}
}, 'UploadStore')

@ -1,3 +0,0 @@
import WebTorrent from 'webtorrent'
export default new WebTorrent({})

@ -0,0 +1,11 @@
import WebTorrent from 'webtorrent'
import socket from 'filepizza-socket'
export function getClient() {
return new Promise((resolve, reject) => {
socket.emit('rtcConfig', {}, (rtcConfig) => {
const client = new WebTorrent({ rtcConfig: rtcConfig })
resolve(client)
})
})
}

@ -36,6 +36,7 @@
"socket.io": "^1.3.5",
"socket.io-client": "^1.3.5",
"stylus": "^0.50.0",
"twilio": "^2.3.0",
"webrtcsupport": "^2.1.2",
"webtorrent": "^0.56.0",
"winston": "^1.0.1",

Loading…
Cancel
Save