Skip to content

Instantly share code, notes, and snippets.

@aashidham
Created October 26, 2016 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aashidham/dd33b949d8ad4eb9ba57301b7888065e to your computer and use it in GitHub Desktop.
Save aashidham/dd33b949d8ad4eb9ba57301b7888065e to your computer and use it in GitHub Desktop.
Fails with 'connection closed'
var Client = require('stratum').Client;
client = Client.create();
//must be specified per EventEmitter requirements
client.on('error', function(socket){
socket.destroy();
console.log('Connection closed');
process.exit(1);
});
// this usually happens when we are not authorized to send commands (the server didn't allow us)
// or share was rejected
// Stratum errors are usually an array with 3 items [int, string, null]
client.on('mining.error', function(msg, socket){
console.log(msg);
});
var submitted = false;
// the client is a one-way communication, it receives data from the server after issuing commands
client.on('mining', function(data, socket, type){
// type will be either 'broadcast' or 'result'
console.log('Mining data: ' + type + ' = ', data);
// you can issue more commands to the socket, it's the exact same socket as "client" variable
// in this example
// the socket (client) got some fields like:
// client.name = name of the worker
// client.authorized = if the current connection is authorized or not
// client.id = an UUID ([U]niversal [U]nique [ID]entifier) that you can safely rely on it's uniqueness
// client.subscription = the subscription data from the server
switch (data.method) {
case 'set_difficulty':
// server sent the new difficulty
break;
case 'notify':
// server sent a new block
break;
default:
if (!socket.authorized){
console.log('Asking for authorization');
socket.stratumAuthorize('user','x');
} else {
console.log('We are authorized');
if (!submitted) {
console.log('Lets submit fake data once for test purposes, then close');
socket.stratumSubmit('', '' ,'' ,' ', '').done(function(){
client.destroy(); // after call to destroy, "client" doesnt exist anymore
});
submitted = true;
}
}
}
});
console.log("about to connect:");
client.connect({
host: 'stratum+tcp://us.clevermining.com',
port: 3333
}).then(function (socket){
// defered, this can be chained if needed, no callback hell
// "socket" refers to the current client, in this case, the 'client'
// variable
console.log('Connected! lets ask for subscribe');
// After the first stratumSubscribe, the data will be handled internally
// and returned deferreds to be resolved / rejected through the event 'mining'
// above
socket.stratumSubscribe().then(
// This isn't needed, it's handled automatically inside the Client class
// but if you want to deal with anything special after subscribing and such.
function(socket){
console.log('Sent!');
},
function(error){
console.log('Error');
}
);
})
// this can be chained and is the last callback to execute
//.done(function(){ });
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment