java websockets onMessage返回输入流到前端javascript



我遇到了一些帖子,没有一个真正回答了我的问题,我也一直在提出这个问题的过程中,但把它推迟了,只是为了研究更多的死胡同…

那么问题来了,使用j2ssh库建立ssh连接:

@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) {
    }
}

我想做的是让handleMessage返回整个inputStream

然后在客户端网页上的javascript中接收输入流:

...
    webSocket.onmessage=function(message) {processMessage(message);};
var THRESHOLD = 10240;
...
    function processMessage(message) {
        //webSocket.send(textMessage.value);
        //textMessage.value="";
        messagesTextarea.value +=" woot woot wootn";
        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);
        */
    }

目标是启动ssh连接(实际上不需要输入-尽管有输入将有利于进一步的客户端调用…

一旦连接到运行

handleMessage(session.getInputStream(),usersession)

这将从连接返回流到javascript前端。

目前我可以看到一个连接到handleMessage,但没有返回到javascript前端…

所以我猜这与如何通过javascript前端传输输入流有关.....

我现在已经在做大量的测试了:

Java端点:

@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)
                }
            }
    }

前端Java脚本(注意调用没有击中数组)

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"
        }
    }

相关内容

  • 没有找到相关文章

最新更新