function BitTorrentConnection (ip,port,hash) { this.ip = ip; this.port = port; this.info_hash = hash; this.on_connect = function (peer_id) { this.peer_id = peer_id; } this.on_error = function (error) { print("connection error: "+error); } this.on_disconnect = function () { print ("disconnected"); } this.on_connect = function (peer_id){ this.peer_id = peer_id; this.on_message = on_message; } this.on_message = function (msgid,evbuf) { if (this.on_msg[msgid]) this.on_msg[msgid].call (this,evbuf); else this.on_default (msgid,evbuf); } this.on_msg = []; this.on_msg[0] = function on_choke () { } this.on_msg[1] = function on_unchoke () { } this.on_msg[2] = function on_interested () { } this.on_msg[3] = function on_uninterested () { } this.on_msg[4] = function on_have () { } this.on_msg[5] = function on_bitfield () { } this.on_msg[6] = function on_request () { } this.on_msg[7] = function on_piece () { } this.on_msg[8] = function on_cancel () { } this.on_msg[9] = function on_trash () { } } BitTorrentConnection.prototype = new BTConnection(); var btcon = new BitTorrentConnection("127.0.0.1","5000", "1ddafb1ff30fada59ef41de609fc91600ce126da");