Skip to content

Commit

Permalink
fix(p2p): fix disconnects after each call
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Feb 17, 2018
1 parent da7ea2a commit 12d66ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/app/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export default class Search extends Component {

window.torrentSocket.emit('searchFiles', oldSearch ? this.currentSearch : this.searchValue, searchFilesParams, window.customLoader((torrents) => {
if(torrents) {
console.log('back torrents')
this.searchFiles = torrents;
let files = 0;
torrents.forEach((torrent) => {
Expand Down Expand Up @@ -166,6 +167,7 @@ export default class Search extends Component {
orderDesc: this.state.orderDesc,
}, window.customLoader((torrents) => {
if(torrents) {
console.log('back torrents')
this.searchTorrents = this.searchTorrents.concat(torrents);
if(torrents.length != this.searchLimit)
this.moreSearchTorrents = false;
Expand Down Expand Up @@ -213,12 +215,20 @@ export default class Search extends Component {
};
window.torrentSocket.emit('statistic', window.customLoader(this.newStatisticFunc));
window.torrentSocket.on('newStatistic', this.newStatisticFunc);

this.remoteSearchTorrent = (torrents) => {
console.log(torrents)
}
window.torrentSocket.on('remoteSearchTorrent', this.remoteSearchTorrent);
}
componentWillUnmount()
{
if(this.newStatisticFunc)
window.torrentSocket.off('newStatistic', this.newStatisticFunc);

if(this.remoteSearchTorrent)
window.torrentSocket.off('remoteSearchTorrent', this.remoteSearchTorrent);

session = {
searchTorrents: this.searchTorrents,
searchFiles: this.searchFiles,
Expand Down
25 changes: 19 additions & 6 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,11 @@ tcpServer.listen(config.spiderPort);
tcpServer.on('connection', (socket) => {
socket = new JsonSocket(socket);
socket.on('message', (message) => {
console.log(message)
if(message.type && messageHandlers[message.type])
{
messageHandlers[message.type](message.data, (data) => {
socket.sendEndMessage({
socket.sendMessage({
id: message.id,
data
});
Expand Down Expand Up @@ -428,10 +429,11 @@ tcpServer.on('connection', (socket) => {
});
}

recive('searchTorrent', (...data) => {
searchTorrentCall(...data)
p2p.emit('searchTorrent', {text: data[0], navigation: data[1]}, (remote) => {
console.log('remote responce', remote)
recive('searchTorrent', (text, navigation, callback) => {
searchTorrentCall(text, navigation, callback)
p2p.emit('searchTorrent', {text, navigation}, (remote) => {
console.log('remote search responce')
send('remoteSearchTorrent', remote)
})
});

Expand Down Expand Up @@ -1043,11 +1045,11 @@ const p2p = {
},
connect(address)
{
this.peers.push(address)
const socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket
socket.connect(address.port, address.address);
socket.on('connect', () => { //Don't send until we're connected
// add to peers
this.peers.push(address)
send('peer', this.peers.length)
console.log('new peer', address)

Expand All @@ -1072,6 +1074,17 @@ const p2p = {
}
address.emit = emit
});

socket.on('close', () => {
const index = this.peers.indexOf(address);
if(index >= 0)
{
this.peers.splice(index, 1);

console.log('close peer connection', address)
send('peer', this.peers.length)
}
})
},
emit(type, data, callback)
{
Expand Down

0 comments on commit 12d66ff

Please sign in to comment.