Commit e9704ec6 authored by Andrey Kondratyev's avatar Andrey Kondratyev
Browse files

chages in onMessage

parent 64ee6068
...@@ -14,7 +14,7 @@ public class JShWebsocket { ...@@ -14,7 +14,7 @@ public class JShWebsocket {
WebsocketBox box = new WebsocketBox(); WebsocketBox box = new WebsocketBox();
box.WebSocketConnect(); box.WebSocketConnect();
box.SendCommand("ls -la"); //box.SendCommand("ls");
} }
......
...@@ -55,7 +55,7 @@ public class WebsocketBox { ...@@ -55,7 +55,7 @@ public class WebsocketBox {
while(true) while(true)
{ {
wse.onMessage(message); wse.SendCmd(message);
Thread.sleep(30000); Thread.sleep(30000);
} }
} }
......
...@@ -25,42 +25,64 @@ import javax.websocket.MessageHandler; ...@@ -25,42 +25,64 @@ import javax.websocket.MessageHandler;
import javax.websocket.RemoteEndpoint; import javax.websocket.RemoteEndpoint;
@ClientEndpoint
public class WebSocketEndPoint extends Endpoint { public class WebSocketEndPoint extends Endpoint {
public static boolean connected = false; private long _startTime = 0L;
public Session session =null; public long getUptime() {
return System.currentTimeMillis() - _startTime;
}
public Session session;
@OnOpen @Override
public void onOpen(Session session, EndpointConfig config) public void onOpen(Session session, EndpointConfig config)
{ {
this.session = session; this.session = session;
connected = true;
System.out.println("Connected to JCentral."); System.out.println("Connected to JCentral.");
//RemoteEndpoint.Basic remoteEndpointBasic = session.getBasicRemote(); RemoteEndpoint.Basic remoteEndpointBasic = session.getBasicRemote();
//session.addMessageHandler(new EchoMessageHandlerText(remoteEndpointBasic)); session.addMessageHandler(new CommandMessageHandler(remoteEndpointBasic));
try {
session.getBasicRemote().sendText("ls");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Thread() {
@Override
public void run() {
try {
Thread.sleep(11 * 60 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(getUptime()<600000)
onClose(session, new CloseReason(null, "Connection expired (run for more than 10 minutes)"));
}
}.start();
} }
@SuppressWarnings("unchecked")
@OnMessage @OnMessage
public void onMessage(String message) throws Exception public void Message(String msg, Session session) {
{ this.session=session;
try {
String cmd = message; session.getBasicRemote().sendText(msg);
String[] tmp; } catch (IOException e) {
tmp = cmd.split(" "); // TODO Auto-generated catch block
JSONObject fullCmd = new JSONObject(); e.printStackTrace();
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);
} }
public void SendCmd(String Cmd) {
@OnClose System.out.println("Trying to send: "+Cmd);
Message(Cmd,this.session);
}
@Override
public void onClose(Session session, CloseReason closereason) public void onClose(Session session, CloseReason closereason)
{ {
this.session = session; this.session = session;
...@@ -72,99 +94,26 @@ public class WebSocketEndPoint extends Endpoint { ...@@ -72,99 +94,26 @@ public class WebSocketEndPoint extends Endpoint {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override @Override
public void onError(Session session, Throwable error) public void onError(Session session, Throwable error)
{ {
} }
/* private static class EchoMessageHandlerText implements MessageHandler.Partial<String> { public class CommandMessageHandler implements MessageHandler.Whole<String>{
private final RemoteEndpoint.Basic remoteEndpointBasic; private final RemoteEndpoint.Basic remoteEndpointBasic;
EchoMessageHandlerText(RemoteEndpoint.Basic remoteEndpointBasic) {
CommandMessageHandler(RemoteEndpoint.Basic remoteEndpointBasic){
this.remoteEndpointBasic = remoteEndpointBasic; this.remoteEndpointBasic = remoteEndpointBasic;
} }
@Override
public void onMessage(String message, boolean last) {
try {
if (remoteEndpointBasic != null) {
// Try to parse incoming JSON
Object pobj;
JSONObject jsonObject;
JSONParser parser = new JSONParser();
try {
pobj = parser.parse(new StringReader(message));
jsonObject = (JSONObject) pobj;
} catch (@SuppressWarnings("unused") ParseException e) {
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);
// }
//Get JSONArray list
final JSONArray fullCmd = new JSONArray();
fullCmd.add(jsonObject.get("command"));
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));
}
//Convert JSONArray to StringArray
final ArrayList<String> sendCommand = new ArrayList<>();
for (int i=0; i<fullCmd.size();i++)
{
sendCommand.add(fullCmd.get(i).toString());
}
//Convert StringArray to String
StringBuilder builder = new StringBuilder();
for (String s: sendCommand)
{
builder.append(s);
}
String finalCmd = builder.toString();
//Send Command
if(connected == true)
{
System.out.println(finalCmd);
remoteEndpointBasic.sendText(finalCmd, last);
}
}
} catch (IOException e) { @Override
// TODO Auto-generated catch block public void onMessage(String msg) {
e.printStackTrace(); if(remoteEndpointBasic!=null) {
} catch (Exception e) { System.out.println("Recieved message: "+msg);
e.printStackTrace();
} }
} }
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