fix(p2p): fix disconnects after each call
This commit is contained in:
		| @ -133,6 +133,7 @@ export default class Search extends Component { | |||||||
|      |      | ||||||
|     window.torrentSocket.emit('searchFiles', oldSearch ? this.currentSearch : this.searchValue, searchFilesParams, window.customLoader((torrents) => { |     window.torrentSocket.emit('searchFiles', oldSearch ? this.currentSearch : this.searchValue, searchFilesParams, window.customLoader((torrents) => { | ||||||
|       if(torrents) { |       if(torrents) { | ||||||
|  |         console.log('back torrents') | ||||||
|         this.searchFiles = torrents; |         this.searchFiles = torrents; | ||||||
|         let files = 0; |         let files = 0; | ||||||
|         torrents.forEach((torrent) => { |         torrents.forEach((torrent) => { | ||||||
| @ -166,6 +167,7 @@ export default class Search extends Component { | |||||||
|         orderDesc: this.state.orderDesc, |         orderDesc: this.state.orderDesc, | ||||||
|     }, window.customLoader((torrents) => { |     }, window.customLoader((torrents) => { | ||||||
|       if(torrents) { |       if(torrents) { | ||||||
|  |         console.log('back torrents') | ||||||
|         this.searchTorrents = this.searchTorrents.concat(torrents); |         this.searchTorrents = this.searchTorrents.concat(torrents); | ||||||
|         if(torrents.length != this.searchLimit) |         if(torrents.length != this.searchLimit) | ||||||
|           this.moreSearchTorrents = false; |           this.moreSearchTorrents = false; | ||||||
| @ -213,12 +215,20 @@ export default class Search extends Component { | |||||||
|     }; |     }; | ||||||
|     window.torrentSocket.emit('statistic', window.customLoader(this.newStatisticFunc)); |     window.torrentSocket.emit('statistic', window.customLoader(this.newStatisticFunc)); | ||||||
|     window.torrentSocket.on('newStatistic', this.newStatisticFunc); |     window.torrentSocket.on('newStatistic', this.newStatisticFunc); | ||||||
|  |  | ||||||
|  |     this.remoteSearchTorrent = (torrents) => { | ||||||
|  |       console.log(torrents) | ||||||
|  |     } | ||||||
|  |     window.torrentSocket.on('remoteSearchTorrent', this.remoteSearchTorrent); | ||||||
|   } |   } | ||||||
|   componentWillUnmount() |   componentWillUnmount() | ||||||
|   { |   { | ||||||
|     if(this.newStatisticFunc) |     if(this.newStatisticFunc) | ||||||
|       window.torrentSocket.off('newStatistic', this.newStatisticFunc); |       window.torrentSocket.off('newStatistic', this.newStatisticFunc); | ||||||
|  |  | ||||||
|  |     if(this.remoteSearchTorrent) | ||||||
|  |       window.torrentSocket.off('remoteSearchTorrent', this.remoteSearchTorrent); | ||||||
|  |  | ||||||
|     session = { |     session = { | ||||||
|       searchTorrents: this.searchTorrents, |       searchTorrents: this.searchTorrents, | ||||||
|       searchFiles: this.searchFiles, |       searchFiles: this.searchFiles, | ||||||
|  | |||||||
| @ -264,10 +264,11 @@ tcpServer.listen(config.spiderPort); | |||||||
| tcpServer.on('connection', (socket) => { | tcpServer.on('connection', (socket) => { | ||||||
| 	socket = new JsonSocket(socket); | 	socket = new JsonSocket(socket); | ||||||
| 	socket.on('message', (message) => {     | 	socket.on('message', (message) => {     | ||||||
|  | 		console.log(message) | ||||||
| 		if(message.type && messageHandlers[message.type]) | 		if(message.type && messageHandlers[message.type]) | ||||||
| 		{ | 		{ | ||||||
| 			messageHandlers[message.type](message.data, (data) => { | 			messageHandlers[message.type](message.data, (data) => { | ||||||
| 				socket.sendEndMessage({ | 				socket.sendMessage({ | ||||||
| 					id: message.id, | 					id: message.id, | ||||||
| 					data | 					data | ||||||
| 				}); | 				}); | ||||||
| @ -428,10 +429,11 @@ tcpServer.on('connection', (socket) => { | |||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	recive('searchTorrent', (...data) => { | 	recive('searchTorrent', (text, navigation, callback) => { | ||||||
| 		searchTorrentCall(...data) | 		searchTorrentCall(text, navigation, callback) | ||||||
| 		p2p.emit('searchTorrent', {text: data[0], navigation: data[1]}, (remote) => { | 		p2p.emit('searchTorrent', {text, navigation}, (remote) => { | ||||||
| 			console.log('remote responce', remote) | 			console.log('remote search responce') | ||||||
|  | 			send('remoteSearchTorrent', remote) | ||||||
| 		}) | 		}) | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| @ -1043,11 +1045,11 @@ const p2p = { | |||||||
| 	}, | 	}, | ||||||
| 	connect(address) | 	connect(address) | ||||||
| 	{ | 	{ | ||||||
|  | 		this.peers.push(address) | ||||||
| 		const socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket | 		const socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket | ||||||
| 		socket.connect(address.port, address.address); | 		socket.connect(address.port, address.address); | ||||||
| 		socket.on('connect', () => { //Don't send until we're connected | 		socket.on('connect', () => { //Don't send until we're connected | ||||||
| 			// add to peers | 			// add to peers | ||||||
| 			this.peers.push(address) |  | ||||||
| 			send('peer', this.peers.length) | 			send('peer', this.peers.length) | ||||||
| 			console.log('new peer', address) | 			console.log('new peer', address) | ||||||
|  |  | ||||||
| @ -1072,6 +1074,17 @@ const p2p = { | |||||||
| 			} | 			} | ||||||
| 			address.emit = emit | 			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) | 	emit(type, data, callback) | ||||||
| 	{ | 	{ | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user