htsp.js, help a poor coder ;(
Added by daniel magnusson over 12 years ago
Tried to convert the htsp hello method from johns android project to javascript. Did not go to well, anyone see what went wrong?
Goal is to get a "Welcomed client software: org.me.tvhguide" on the tvheadend server..
I get;
[INFO]:htsp: Got connection from 192.168.1.101
[INFO]:htsp: 192.168.1.101: Disconnected
I want;
[INFO]:htsp: 192.168.1.101: Welcomed client software: org.me.tvhguide
(Can i enable more logs?)
code;
var net = require('net'); var HOST = '192.168.1.102'; var PORT = 9982; function serializeBinary(key, value) { var type = 2; if (typeof value == "number") { type = 2; } else if (typeof value == "string") { type = 3; } var bName = new Buffer(key); var bData = new Buffer(value); var buf = new Array(6) buf[0] = type; buf[1] = (bName.length & 0xFF); buf[2] = ((bData.length >> 24) & 0xFF); buf[3] = ((bData.length >> 16) & 0xFF); buf[4] = ((bData.length >> 8) & 0xFF); buf[5] = ((bData.length) & 0xFF); var ch, st = []; for (var i = 0; i < key.length; i++) { ch = key.charCodeAt(i); st = []; // set up "stack" do { st.push(ch & 0xFF); // push byte to stack ch = ch >> 8; // shift value down by 1 byte } while (ch); // add stack contents to result // done because chars have "wrong" endianness buf = buf.concat(st.reverse()); } if (typeof value == "string") { var ch, st = []; for (var i = 0; i < value.length; i++) { ch = value.charCodeAt(i); // get char st = []; // set up "stack" do { st.push(ch & 0xFF); // push byte to stack ch = ch >> 8; // shift value down by 1 byte } while (ch); // add stack contents to result // done because chars have "wrong" endianness buf = buf.concat(st.reverse()); } } if (typeof value == "number") { buf = buf.concat(value); } return buf; } var client = new net.Socket(); client.setTimeout(5000); client.setNoDelay(noDelay=true); client.setKeepAlive(enable=true); client.connect(PORT, HOST, function() { console.log('CONNECTED TO: ' + HOST + ':' + PORT); var buffer = new Array() var sub = serializeBinary('seq', 1); console.log('\nseq:1'); console.log('expect: 2,3,0,0,0,1,115,101,113,1'); console.log('sub: ' + sub); buffer = buffer.concat(sub); console.log('buffer: ' + buffer); var sub = serializeBinary('clientversion', '1.0'); console.log('\nclientversion:1.0'); console.log('expect: 3,13,0,0,0,3,99,108,105,101,110,116,118,101,114,115,105,111,110,49,46,48'); console.log('sub: ' + sub); buffer = buffer.concat(sub); console.log('buffer: ' + buffer); var sub = serializeBinary('username', ''); console.log('\nusername:'); console.log('expect: 3,8,0,0,0,0,117,115,101,114,110,97,109,101'); console.log('sub: ' + sub); buffer = buffer.concat(sub); console.log('buffer: ' + buffer); var sub = serializeBinary('htspversion', 4); console.log('\nhtspversion:4'); console.log('expect: 2,11,0,0,0,1,104,116,115,112,118,101,114,115,105,111,110,4'); console.log('sub: ' + sub); buffer = buffer.concat(sub); console.log('buffer: ' + buffer); var sub = serializeBinary('method', 'hello'); console.log('\nmethod:hello'); console.log('expect: 3,6,0,0,0,5,109,101,116,104,111,100,104,101,108,108,111'); console.log('sub: ' + sub); buffer = buffer.concat(sub); console.log('buffer: ' + buffer); var sub = serializeBinary('clientname', 'org.me.tvhguide'); console.log('\nclientname:org.me.tvhguide'); console.log('expect: 3,10,0,0,0,15,99,108,105,101,110,116,110,97,109,101,111,114,103,46,109,101,46,116,118,104,103,117,105,100,101'); console.log('sub: ' + sub); buffer = buffer.concat(sub); console.log('\nbuffer1: ' + buffer); //flip buffer.reverse().join(""); console.log('\nbuffer2: ' + buffer); var dataBuffer = new Array(4) dataBuffer[0] = ((buffer.length >> 24) & 0xFF); dataBuffer[1] = ((buffer.length >> 16) & 0xFF); dataBuffer[2] = ((buffer.length >> 8) & 0xFF); dataBuffer[3] = ((buffer.length) & 0xFF); dataBuffer = dataBuffer.concat(buffer); console.log('\ndataBuffer1: ' + dataBuffer); //flip dataBuffer.reverse().join(""); console.log('\ndataBuffer2: ' + dataBuffer); console.log('\ndataBuffer.length: ' + dataBuffer.length); var msg = new Buffer(dataBuffer); client.write(msg); client.pause(); }); client.on('connect', function(exception) { console.log('after connect'); }); client.on('data', function(data) { console.log('DATA: ' + data); }); client.on('error', function(exception) { console.log('Connection exception'); }); client.on('timeout', function() { console.log('Connection timeout'); }); client.on('drain', function() { console.log('Connection drain'); }); client.on('end', function() { console.log('Connection end'); }); client.on('close', function(had_error) { console.log('Connection closed, had_error:' + had_error); });