我发现了一些帖子,其中没有一个帖子真的回答了我的问题,我也一直在提出这个问题但是只是为了研究更多死胡同......
所以这就是问题,使用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 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);
*/
}
目标是启动ssh连接(实际上不需要输入 - 尽管输入会对进一步的客户端调用有益...
连接到运行
handleMessage(session.getInputStream(),usersession)
然后将流从连接返回到javascript前端。
目前我可以看到与handleMessage的连接,但没有任何内容返回到javascript前端......
所以我猜它与你如何通过javascript前端传输输入流有关.....
答案 0 :(得分:0)
我实际上已经开始工作 - 目前正在进行大量测试:
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"
}
}