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,36 +34,38 @@ export default alt.createStore(class DownloadStore {
if (this.status !== 'ready') return
this.status = 'requesting'
webtorrent.download(this.infoHash, (torrent) => {
this.setState({ status: 'downloading' })
getClient().then((client) => {
client.download(this.infoHash, (torrent) => {
this.setState({ status: 'downloading' })
const updateSpeed = () => {
this.setState({
speedUp: torrent.swarm.uploadSpeed(),
speedDown: torrent.swarm.downloadSpeed(),
peers: torrent.swarm.wires.length
})
}
const updateSpeed = () => {
this.setState({
speedUp: torrent.swarm.uploadSpeed(),
speedDown: torrent.swarm.downloadSpeed(),
peers: torrent.swarm.wires.length
})
}
torrent.on('upload', updateSpeed)
torrent.on('download', updateSpeed)
setInterval(updateSpeed, SPEED_REFRESH_TIME)
torrent.on('upload', updateSpeed)
torrent.on('download', updateSpeed)
setInterval(updateSpeed, SPEED_REFRESH_TIME)
const file = torrent.files[0]
const stream = file.createReadStream()
stream.on('data', (chunk) => {
if (this.status !== 'downloading') return
const file = torrent.files[0]
const stream = file.createReadStream()
stream.on('data', (chunk) => {
if (this.status !== 'downloading') return
if (torrent.progress === 1) {
this.setState({ status: 'done', progress: 1 })
file.getBlobURL((err, blobURL) => {
if (err) throw err
downloadBlobURL(this.fileName, blobURL)
})
} else {
this.setState({ progress: torrent.progress })
}
if (torrent.progress === 1) {
this.setState({ status: 'done', progress: 1 })
file.getBlobURL((err, blobURL) => {
if (err) throw err
downloadBlobURL(this.fileName, blobURL)
})
} else {
this.setState({ progress: torrent.progress })
}
})
})
})
}

@ -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,35 +24,37 @@ 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({
speedUp: torrent.swarm.uploadSpeed(),
peers: torrent.swarm.wires.length
})
}
torrent.on('upload', updateSpeed)
torrent.on('download', updateSpeed)
setInterval(updateSpeed, SPEED_REFRESH_TIME)
socket.emit('upload', {
fileName: file.name,
fileSize: file.size,
fileType: file.type,
infoHash: torrent.infoHash
}, (token) => {
this.setState({
status: 'uploading',
token: token,
const updateSpeed = () => {
this.setState({
speedUp: torrent.swarm.uploadSpeed(),
peers: torrent.swarm.wires.length
})
}
torrent.on('upload', updateSpeed)
torrent.on('download', updateSpeed)
setInterval(updateSpeed, SPEED_REFRESH_TIME)
socket.emit('upload', {
fileName: file.name,
fileSize: file.size,
fileType: file.type,
infoHash: torrent.infoHash
}, (token) => {
this.setState({
status: 'uploading',
token: token,
fileName: file.name,
fileSize: file.size,
fileType: file.type,
infoHash: torrent.infoHash
})
})
})
})
})
}

@ -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