|
@@ -1,10 +1,9 @@
|
|
package org.robert.printer.ws;
|
|
package org.robert.printer.ws;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.io.Reader;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
+import javax.print.PrintException;
|
|
|
|
|
|
import org.eclipse.jetty.websocket.api.Session;
|
|
import org.eclipse.jetty.websocket.api.Session;
|
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
|
@@ -12,7 +11,9 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
|
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
|
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
|
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
|
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
|
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
|
|
|
+import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.json.JSONObject;
|
|
|
|
+import org.robert.printer.common.PrinterProcessor;
|
|
import org.robert.printer.common.PrintersDiscovery;
|
|
import org.robert.printer.common.PrintersDiscovery;
|
|
|
|
|
|
@WebSocket
|
|
@WebSocket
|
|
@@ -60,8 +61,8 @@ public class PrintersSocketHandler {
|
|
}
|
|
}
|
|
|
|
|
|
@OnWebSocketMessage
|
|
@OnWebSocketMessage
|
|
- public void onMessage(Session session, Reader reader) throws IOException {
|
|
|
|
- String message = IOUtils.toString(reader);
|
|
|
|
|
|
+ public void onMessage(Session session, String message) throws IOException {
|
|
|
|
+ System.out.println("------>" + message);
|
|
|
|
|
|
if (message == null || message.isEmpty()) {
|
|
if (message == null || message.isEmpty()) {
|
|
return;
|
|
return;
|
|
@@ -86,6 +87,26 @@ public class PrintersSocketHandler {
|
|
JSONObject printerNames = PrintersDiscovery.getPrinterNames();
|
|
JSONObject printerNames = PrintersDiscovery.getPrinterNames();
|
|
this.send(session, printerNames);
|
|
this.send(session, printerNames);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (action.equals("print")) {
|
|
|
|
+ String printer = json.optString("printer");
|
|
|
|
+ String commands = json.optString("data");
|
|
|
|
+
|
|
|
|
+ PrinterProcessor processor = new PrinterProcessor();
|
|
|
|
+ try {
|
|
|
|
+ processor.print(printer, commands);
|
|
|
|
+
|
|
|
|
+ JSONObject ok = new JSONObject();
|
|
|
|
+ ok.append("status", "ok");
|
|
|
|
+
|
|
|
|
+ this.send(session, ok);
|
|
|
|
+ } catch (PrintException e) {
|
|
|
|
+ JSONObject error = new JSONObject();
|
|
|
|
+ error.append("error", e.getMessage());
|
|
|
|
+
|
|
|
|
+ this.send(session, error);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|