fix(p2p): fix disconnects after each call
This commit is contained in:
parent
91f5a3fabd
commit
98f0404869
@ -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) => {
|
||||
@ -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;
|
||||
@ -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,
|
||||
|
@ -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
|
||||
});
|
||||
@ -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)
|
||||
})
|
||||
});
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user