Commit 64ee6068 authored by Andrey Kondratyev's avatar Andrey Kondratyev
Browse files

changed onMessage method

parent 5d465a01
package alien;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;
import alien.config.ConfigUtils;
import alien.config.JAliEnIAm;
import alien.shell.BusyBox;
import alien.shell.ShellColor;
import alien.shell.commands.JAliEnBaseCommand;
import alien.user.UserFactory;
import lazyj.commands.CommandOutput;
import lazyj.commands.SystemCommand;
import lia.util.process.ExternalProcess.ExitStatus;
import utils.ProcessWithTimeout;
import alien.shell.WebsocketBox;
import alien.websockets.WebSocketEndPoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.net.URI;
import alien.websockets.EndpointConfig;
import alien.websockets.JsonWebsocketEndpoint;
import org.apache.tomcat.websocket.WsWebSocketContainer;
public class JShWebsocket {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
WebsocketBox box = new WebsocketBox();
box.WebSocketConnect();
new Thread()
{
}.start();
box.SendCommand("ls -la");
}
}
package alien.shell;
import alien.websockets.WebSocketConnection;
import alien.websockets.WebSocketEndPoint;
import jline.console.ConsoleReader;
import jline.console.completer.ArgumentCompleter;
import jline.console.completer.StringsCompleter;
import javax.websocket.Session;
import org.json.simple.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
......@@ -24,12 +27,12 @@ public class WebsocketBox {
private OutputStream os;
public void SendCommand(final String... commandAndArguments) throws IOException
/*public void SendCommand(final String... commandAndArguments) throws IOException
{
final String cmd = commandAndArguments.toString();
os.write(cmd.getBytes());
os.flush();
}
}*/
public boolean WebSocketConnect()
{
......@@ -45,12 +48,15 @@ public class WebsocketBox {
return false;
}
public void Disconnect()
public void SendCommand(String message) throws Exception
{
if (WebSocketConnect()==true)
System.out.println("Start sending message");
WebSocketEndPoint wse = new WebSocketEndPoint();
while(true)
{
wse.onMessage(message);
Thread.sleep(30000);
}
}
......
package alien.websockets;
import javax.websocket.Endpoint;
import javax.websocket.Session;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.OnClose;
import org.apache.catalina.startup.Tomcat;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import alien.api.TomcatServer;
import alien.shell.commands.JAliEnCOMMander;
import alien.shell.commands.UIPrintWriter;
import alien.user.AliEnPrincipal;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.EncodeException;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.OnMessage;
import javax.websocket.RemoteEndpoint;
import alien.shell.commands.JAliEnCOMMander;
import alien.shell.commands.JSONPrintWriter;
import alien.shell.commands.JShPrintWriter;
import alien.shell.commands.UIPrintWriter;
import alien.shell.commands.XMLPrintWriter;
import alien.user.AliEnPrincipal;
@ClientEndpoint
public class WebSocketEndPoint extends Endpoint {
public static boolean connected = false;
public Session session =null;
@Override
@OnOpen
public void onOpen(Session session, EndpointConfig config)
{
this.session = session;
connected = true;
System.out.println("Connected to JCentral.");
RemoteEndpoint.Basic remoteEndpointBasic = session.getBasicRemote();
session.addMessageHandler(new EchoMessageHandlerText(remoteEndpointBasic));
//RemoteEndpoint.Basic remoteEndpointBasic = session.getBasicRemote();
//session.addMessageHandler(new EchoMessageHandlerText(remoteEndpointBasic));
}
@Override
public void onClose(Session session, CloseReason closereason)
@SuppressWarnings("unchecked")
@OnMessage
public void onMessage(String message) throws Exception
{
String cmd = message;
String[] tmp;
tmp = cmd.split(" ");
JSONObject fullCmd = new JSONObject();
fullCmd.put("command", tmp[0]);
for (int i=1; i<tmp.length;i++) {
fullCmd.put("options",tmp[i].toString());
}
System.out.println("Command is: "+fullCmd.toString());
this.session.getBasicRemote().sendObject(fullCmd);
}
@OnClose
public void onClose(Session session, CloseReason closereason)
{
this.session = session;
try {
if (session != null)
session.close();
......@@ -72,7 +80,7 @@ public class WebSocketEndPoint extends Endpoint {
}
private static class EchoMessageHandlerText implements MessageHandler.Partial<String> {
/* private static class EchoMessageHandlerText implements MessageHandler.Partial<String> {
private final RemoteEndpoint.Basic remoteEndpointBasic;
EchoMessageHandlerText(RemoteEndpoint.Basic remoteEndpointBasic) {
......@@ -95,23 +103,23 @@ public class WebSocketEndPoint extends Endpoint {
remoteEndpointBasic.sendText("Incoming JSON not ok", last);
return;
}
/*
// Split JSONObject into strings
final ArrayList<String> fullCmd = new ArrayList<>();
fullCmd.add(jsonObject.get("command").toString());
JSONArray mArray = new JSONArray();
if (jsonObject.get("options") != null) {
mArray = (JSONArray) jsonObject.get("options");
for (int i = 0; i < mArray.size(); i++)
fullCmd.add(mArray.get(i).toString());
}
for(String m: fullCmd)
{
//session.getBasicRemote().sendText(fullCmd.toArray(new String[i]));
remoteEndpointBasic.sendText(m,last);
}*/
//
// // Split JSONObject into strings
// final ArrayList<String> fullCmd = new ArrayList<>();
// fullCmd.add(jsonObject.get("command").toString());
//
// JSONArray mArray = new JSONArray();
// if (jsonObject.get("options") != null) {
// mArray = (JSONArray) jsonObject.get("options");
//
// for (int i = 0; i < mArray.size(); i++)
// fullCmd.add(mArray.get(i).toString());
// }
// for(String m: fullCmd)
// {
// //session.getBasicRemote().sendText(fullCmd.toArray(new String[i]));
// remoteEndpointBasic.sendText(m,last);
// }
//Get JSONArray list
final JSONArray fullCmd = new JSONArray();
......@@ -154,5 +162,9 @@ public class WebSocketEndPoint extends Endpoint {
}
}
}
public void Send(String message)
{
onMessage(message, true);
}
}*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment