I'm new to javascript to I'm probably asking something easy. I'm trying to develop a project using SockJS, nodeJs and ssh2 module of nodeJs.
I'm trying to send a message from Chrome using SockJS library's client side code (websockets) to the NodeJS server and then from the NodeJs to my raspberry pi via ssh2 module. The problem is the following one:
var http = require('http');
var sockjs = require('sockjs');
var Connection = require('ssh2');
var c = new Connection();
var sockserver = sockjs.createServer();
var server = http.createServer();
sockserver.installHandlers(server, {prefix:'/socksvr'});
server.listen(9999, '0.0.0.0');
sockserver.on('connection', function(conn) {
c.connect({
// Parameters to connect
});
//Here goes some code to check that the connection was successful
conn.on('data', function(message) {
//this is when Node receives data from client socket
//print somethings
c.shell(onShell); // This is to send mand from node to Raspberry Pi
});
//Here goes the problem
var onShell = function(err, stream) {
if (err != null) {
console.log('error: ' + err);
}
stream.on('data', function(data, extended) {
if (extended === 'stderr'){
console.log('STDERR: '+data);
conn.write(data);
}else{
if(data != ''){
console.log(':' + data);
conn.write(':' + data);
}
};
});
stream.write(message);
}
});
\server.js:62
stream.write(message);
^
ReferenceError: message is not defined
at onShell (C:\Documents and Settings\...\server.js:62:18)
at C:\Documents and Settings\...\node_modules\ssh2\lib\Channel.js:277:5
at Parser.<anonymous> (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Channel.js:123:30)
at Parser.EventEmitter.emit (events.js:92:17)
at Parser.parsePacket (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Parser.js:594:12)
at Parser.execute (C:\Documents and Settings\...\node_modules\ssh2\lib\P
arser.js:238:14)
at Socket.<anonymous> (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Connection.js:488:18)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
I knew that using message var inside that function wasn't probably going to work but I haven't been able to figure out the solution. I'm probably lacking some knowledge about functions. Any help is appreciated. Thank you.
I'm new to javascript to I'm probably asking something easy. I'm trying to develop a project using SockJS, nodeJs and ssh2 module of nodeJs.
I'm trying to send a message from Chrome using SockJS library's client side code (websockets) to the NodeJS server and then from the NodeJs to my raspberry pi via ssh2 module. The problem is the following one:
var http = require('http');
var sockjs = require('sockjs');
var Connection = require('ssh2');
var c = new Connection();
var sockserver = sockjs.createServer();
var server = http.createServer();
sockserver.installHandlers(server, {prefix:'/socksvr'});
server.listen(9999, '0.0.0.0');
sockserver.on('connection', function(conn) {
c.connect({
// Parameters to connect
});
//Here goes some code to check that the connection was successful
conn.on('data', function(message) {
//this is when Node receives data from client socket
//print somethings
c.shell(onShell); // This is to send mand from node to Raspberry Pi
});
//Here goes the problem
var onShell = function(err, stream) {
if (err != null) {
console.log('error: ' + err);
}
stream.on('data', function(data, extended) {
if (extended === 'stderr'){
console.log('STDERR: '+data);
conn.write(data);
}else{
if(data != ''){
console.log(':' + data);
conn.write(':' + data);
}
};
});
stream.write(message);
}
});
\server.js:62
stream.write(message);
^
ReferenceError: message is not defined
at onShell (C:\Documents and Settings\...\server.js:62:18)
at C:\Documents and Settings\...\node_modules\ssh2\lib\Channel.js:277:5
at Parser.<anonymous> (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Channel.js:123:30)
at Parser.EventEmitter.emit (events.js:92:17)
at Parser.parsePacket (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Parser.js:594:12)
at Parser.execute (C:\Documents and Settings\...\node_modules\ssh2\lib\P
arser.js:238:14)
at Socket.<anonymous> (C:\Documents and Settings\...\node_modules\ssh2\l
ib\Connection.js:488:18)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
I knew that using message var inside that function wasn't probably going to work but I haven't been able to figure out the solution. I'm probably lacking some knowledge about functions. Any help is appreciated. Thank you.
Share Improve this question asked Mar 17, 2014 at 9:49 ederolloraederollora 1,1812 gold badges11 silver badges31 bronze badges2 Answers
Reset to default 1Your message variable only exists inside the scope of the conn.on('data', function. You would have to declare message in sockserver.on('connection', or the scope above for it to be visible in both functions.
You are trying to access message
out of scope. You could do this to pass it to onShell
:
conn.on('data', function(message) {
c.shell(function (err, stream) {
onShell(err, stream, message);
});
});
var onShell = function(err, stream, message) {
// Etc...
A function's arguments are only accessible inside the function.