MCPcopy
hub / github.com/webtorrent/bittorrent-tracker / _onWebSocketRequest

Method _onWebSocketRequest

server.js:470–574  ·  view source on GitHub ↗
(socket, opts, params)

Source from the content-addressed store, hash-verified

468 }
469
470 _onWebSocketRequest (socket, opts, params) {
471 try {
472 params = parseWebSocketRequest(socket, opts, params)
473 } catch (err) {
474 socket.send(JSON.stringify({
475 'failure reason': err.message
476 }), socket.onSend)
477
478 // even though it's an error for the client, it's just a warning for the server.
479 // don't crash the server because a client sent bad data :)
480 this.emit('warning', err)
481 return
482 }
483
484 if (!socket.peerId) socket.peerId = params.peer_id // as hex
485
486 this._onRequest(params, (err, response) => {
487 if (this.destroyed || socket.destroyed) return
488 if (err) {
489 socket.send(JSON.stringify({
490 action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape',
491 'failure reason': err.message,
492 info_hash: hex2bin(params.info_hash)
493 }), socket.onSend)
494
495 this.emit('warning', err)
496 return
497 }
498
499 response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape'
500
501 let peers
502 if (response.action === 'announce') {
503 peers = response.peers
504 delete response.peers
505
506 if (!socket.infoHashes.includes(params.info_hash)) {
507 socket.infoHashes.push(params.info_hash)
508 }
509
510 response.info_hash = hex2bin(params.info_hash)
511
512 // WebSocket tracker should have a shorter interval – default: 2 minutes
513 response.interval = Math.ceil(this.intervalMs / 1000 / 5)
514 }
515
516 // Skip sending update back for 'answer' announce messages – not needed
517 if (!params.answer) {
518 socket.send(JSON.stringify(response), socket.onSend)
519 debug('sent response %s to %s', JSON.stringify(response), params.peer_id)
520 }
521
522 if (Array.isArray(params.offers)) {
523 debug('got %s offers from %s', params.offers.length, params.peer_id)
524 debug('got %s peers from swarm %s', peers.length, params.info_hash)
525 peers.forEach((peer, i) => {
526 peer.socket.send(JSON.stringify({
527 action: 'announce',

Callers 1

onWebSocketConnectionMethod · 0.95

Calls 3

_onRequestMethod · 0.95
getSwarmMethod · 0.95
sendMethod · 0.80

Tested by

no test coverage detected