Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have come across a few posts, none of which has really answered my question, I have also been in the process of raising the question but putting it off only to research more dead ends...

So here is the issue, using j2ssh libraries to make ssh connections :

@OnMessage
    public String handleOpen(Session usersession) { 
    System.out.println("Client is now connected.")

    properties = new SshConnectionProperties();
    properties.setHost(host)
    properties.setPort(sshPort)
    ssh.connect(properties, new IgnoreHostKeyVerification())
    int result=0

        PasswordAuthenticationClient pwd = new PasswordAuthenticationClient()
        pwd.setUsername(username)
        pwd.setPassword(password)
        result = ssh.authenticate(pwd)
        if(result == 4)  {
            isAuthenticated=true
        }

    // Evaluate the result
    if (isAuthenticated) {
        session = ssh.openSessionChannel()
        SessionOutputReader sor = new SessionOutputReader(session)
        if (session.requestPseudoTerminal("gogrid",80,24, 0 , 0, "")) {
            if (session.startShell()) {
                ChannelOutputStream out = session.getOutputStream()
                session.getOutputStream().write("${usercommand} \n".getBytes())
                println "--------------------------------authenticated user"
                handleMessage(session.getInputStream(),usersession)
             }
          }
    }

}

@OnMessage
public void handleMessage(InputStream stream, Session usersession) {
    try {

        usersession.getBasicRemote().sendText('SOMETHING')
        usersession.getBasicRemote().sendObject(stream)
        //return stream

    } catch (IOException e) {
    }
}

What I would like to do is for handleMessage to return the entire inputStream

Then within the javascript on the client's webpage to recieve that input stream :

...
    webSocket.onmessage=function(message) {processMessage(message);};
var THRESHOLD = 10240;
...
    function processMessage(message) {
        //webSocket.send(textMessage.value);
        //textMessage.value="";
        messagesTextarea.value +=" woot woot woot\n";
        if(message.data instanceof ArrayBuffer) {

              var wordarray = new Uint16Array(message.data);
              for (var i = 0; i < wordarray.length; i++) 
                    {
                      console.log(wordarray[i]);
                      wordarray[i]=wordarray[i]+1;
                   }

              messagesTextarea.value +=""+wordarray.buffer+"\n"
            }else{
                messagesTextarea.value +=""+message.data+"\n"
            }

        /*
        setInterval( function() {
        //messagesTextarea.value +=" Receive from Server ===> "+ message.data +"\n";
            if (webSocket.bufferedAmount < THRESHOLD)  {
                if ((message.data != null)&&(message.data !="")) { 
                messagesTextarea.value +=" got from Server ===> ("+message.data+")";
                }
            }   }, 5000);
        */
    }

The objective is to launch ssh connection (no input really required - although with input would be of benefit for further client calls...

Once connected to run

handleMessage(session.getInputStream(),usersession)

This then returns the stream from the connection back to the javascript frontend.

At the moment I can see a connection to handleMessage but nothing is being returned to javascript frontend...

So I guess its related to how do you stream inputstreams via a javascript frontend.....

share|improve this question

Ive actually got it working - up for lots of testing at the momemnt:

Java Endpoint:

@OnMessage
    public void handleMessage(String message, Session usersession) {
            InputStream input=session.getInputStream()
            byte[] buffer=new byte[255]
            int read;
            int i=0
            def pattern = ~/^\s+$/
            while((read = input.read(buffer)) > 0)  {
                String out1 = new String(buffer, 0, read)
                def m=pattern.matcher(out1).matches()
                if (m==false) {
                     usersession.getBasicRemote().sendText(out1)
                }
            }
    }

Front end Java script (note the call is not hitting array if call )

function processMessage(message) {
        if(message.data instanceof ArrayBuffer) {
            var wordarray = new Uint16Array(message.data);
            for (var i = 0; i < wordarray.length; i++) {
                console.log(wordarray[i]);
                wordarray[i]=wordarray[i]+1;
            }
            messagesTextarea.value +=""+wordarray.buffer+"\n"
        }else{
            messagesTextarea.value +=""+message.data+"\n"
        }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.