Share WebTorrent client between multiple components

master
Torjus 2018-07-26 16:33:20 +02:00
parent 7112880ac0
commit f64ff1b030
4 changed files with 13 additions and 14 deletions

View File

@ -5,9 +5,17 @@ import * as stateActions from '../../redux/stateActions';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import MessageList from './MessageList'; import MessageList from './MessageList';
import FileSharing from './FileSharing'; import FileSharing from './FileSharing';
import WebTorrent from 'webtorrent';
class Chat extends Component class Chat extends Component
{ {
constructor(props)
{
super(props);
this.client = new WebTorrent();
}
render() render()
{ {
const { const {
@ -21,9 +29,9 @@ class Chat extends Component
return ( return (
<div data-component='Chat'> <div data-component='Chat'>
<MessageList /> <MessageList client={this.client} />
<FileSharing /> <FileSharing client={this.client} />
<form <form
data-component='Sender' data-component='Sender'

View File

@ -4,13 +4,6 @@ import { saveAs } from 'file-saver/FileSaver';
class FileChatEntry extends Component class FileChatEntry extends Component
{ {
constructor(props)
{
super(props);
this.client = new WebTorrent();
}
state = { state = {
active: false, active: false,
numPeers: 0, numPeers: 0,
@ -23,7 +16,7 @@ class FileChatEntry extends Component
active: true active: true
}); });
this.client.add(this.props.message.file.magnet, (torrent) => this.props.client.add(this.props.message.file.magnet, (torrent) =>
{ {
const onProgress = () => const onProgress = () =>
{ {

View File

@ -17,11 +17,9 @@ class FileSharing extends Component {
componentDidMount() componentDidMount()
{ {
this.client = new WebTorrent();
dragDrop('body', (files) => dragDrop('body', (files) =>
{ {
this.client.seed(files, (torrent) => { this.props.client.seed(files, (torrent) => {
this.notifyPeers({ this.notifyPeers({
magnet: torrent.magnetURI magnet: torrent.magnetURI
}); });

View File

@ -74,7 +74,7 @@ class MessageList extends Component
)} )}
{message.type === 'file' && ( {message.type === 'file' && (
<FileChatEntry message={message} /> <FileChatEntry message={message} client={this.props.client} />
)} )}
</div> </div>