List of usage examples for org.apache.wicket.protocol.ws.api.registry IWebSocketConnectionRegistry getConnections
Collection<IWebSocketConnection> getConnections(Application application);
From source file:org.apache.openmeetings.core.util.WebSocketHelper.java
License:Apache License
private static void sendAll(Consumer<IWebSocketConnection> sender) { new Thread(() -> { Application app = (Application) getApp(); if (app == null) { return; // Application is not ready }//from w w w . jav a2 s .c o m WebSocketSettings settings = WebSocketSettings.Holder.get(app); IWebSocketConnectionRegistry reg = settings.getConnectionRegistry(); Executor executor = settings.getWebSocketPushMessageExecutor(); for (IWebSocketConnection c : reg.getConnections(app)) { executor.run(() -> sender.accept(c)); } }).start(); }
From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java
License:Apache License
/** * Mapping JSON String to Objects and Adding to the Element List * //from ww w . ja v a2s .co m * @param editedElement * @return */ private boolean handleEditedElement(String editedElement) { try { JSONObject jsonEditedElement = new JSONObject(editedElement); Element element = getElementObject(jsonEditedElement); boolean isLoaded = false; if (!elementMap.isEmpty() && loadedElementMap.get(element.getId()) != null && !loadedElementMap.isEmpty()) { isLoaded = isEqual(element.getJSON(), loadedElementMap.get(element.getId()).getJSON()); } // If the edited element is not from file loaded content this clause executes if (!isLoaded) { // Adding the element creation/editing is add to the undo snapshots if (snapShot == null && snapShotCreation == null) { snapShot = new ArrayList<Element>(); snapShotCreation = new ArrayList<Boolean>(); } if (elementMap.containsKey(element.getId()) && !elementMap.isEmpty()) { snapShot.add(elementMap.get(element.getId())); snapShotCreation.add(false); } else { snapShot.add(element); snapShotCreation.add(true); } if (Type.PointFree != element.getType()) { if (undoSnapshots.size() == 20) { undoSnapshots.pollFirst(); undoSnapshotCreationList.pollFirst(); } if (Type.PencilCurve == element.getType()) { List<Element> lastElementSnapshot = undoSnapshots.peekLast(); if (lastElementSnapshot != null) { Element lastSnapshotElement = lastElementSnapshot.get(lastElementSnapshot.size() - 1); if ((lastSnapshotElement instanceof PencilCurve) && (lastSnapshotElement.getId() == element.getId())) { List<Boolean> lastCreationSnapshot = undoSnapshotCreationList.getLast(); for (int i = 0; i < snapShot.size(); i++) { lastElementSnapshot.add(snapShot.get(i)); lastCreationSnapshot.add(snapShotCreation.get(i)); } } else { undoSnapshots.addLast(snapShot); undoSnapshotCreationList.addLast(snapShotCreation); isElementSnapshotList.addLast(true); } } } else if (Type.ClipArt == element.getType()) { List<Element> snapShotTemp = undoSnapshots.pollLast(); List<Boolean> snapShotCreationTemp = undoSnapshotCreationList.pollLast(); for (int i = 0; i < snapShotTemp.size(); i++) { snapShot.add(snapShotTemp.get(i)); snapShotCreation.add(snapShotCreationTemp.get(i)); } undoSnapshots.addLast(snapShot); undoSnapshotCreationList.addLast(snapShotCreation); isElementSnapshotList.addLast(true); } else { undoSnapshots.addLast(snapShot); undoSnapshotCreationList.addLast(snapShotCreation); isElementSnapshotList.addLast(true); } snapShot = null; snapShotCreation = null; } // Synchronizing newly added/edited element between whiteboard clients if (element != null) { elementMap.put(element.getId(), element); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()) .getConnectionRegistry(); for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { JSONObject jsonObject = new JSONObject(editedElement); c.sendMessage(getAddElementMessage(jsonObject).toString()); } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } } } return true; } catch (JSONException e) { log.error("Unexpected error while editing element", e); } return false; }
From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java
License:Apache License
/** * Synchronizing newly added/edited background between whiteboard clients * // w w w . jav a2s. com * @param backgroundString * @return */ private boolean handleBackground(String backgroundString) { try { JSONObject backgroundJSON = new JSONObject(backgroundString); Background backgroundObject = new Background(backgroundJSON); background = backgroundObject; Background previousBackground = new Background("Background", "", 0.0, 0.0, 0.0, 0.0); if (whiteboardMap.get(whiteboardObjectId).getBackground() != null) { previousBackground = whiteboardMap.get(whiteboardObjectId).getBackground(); } whiteboardMap.get(whiteboardObjectId).setBackground(background); undoSnapshotCreationList_Background.addLast(previousBackground == null); undoSnapshots_Background.addLast(previousBackground); isElementSnapshotList.addLast(false); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()) .getConnectionRegistry(); for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { JSONObject jsonObject = new JSONObject(backgroundString); c.sendMessage(getAddBackgroundMessage(jsonObject).toString()); } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } return true; } catch (JSONException e) { e.printStackTrace(); } return false; }
From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java
License:Apache License
/** * Undo one step and synchronizing undo between whiteboard clients * /*from ww w.j a va 2 s. c om*/ * @return */ private boolean handleUndo() { if (!isElementSnapshotList.isEmpty()) { if (isElementSnapshotList.pollLast()) { List<Boolean> undoCreationList = undoSnapshotCreationList.pollLast(); List<Element> undoElement = undoSnapshots.pollLast(); String deleteList = ""; JSONArray changeList = new JSONArray(); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()) .getConnectionRegistry(); for (int i = 0; i < undoElement.size(); i++) { if (undoCreationList.get(i)) { elementMap.remove(undoElement.get(i).getId()); if (loadedElementMap.containsKey(undoElement.get(i).getId())) { loadedContent = ""; whiteboardMap.get(whiteboardObjectId).setLoadedContent(""); loadedElementMap.remove(undoElement.get(i).getId()); } if ("".equals(deleteList)) { deleteList = "" + undoElement.get(i).getId(); } else { deleteList += "," + undoElement.get(i).getId(); } } else { elementMap.put(undoElement.get(i).getId(), undoElement.get(i)); try { changeList.put(undoElement.get(i).getJSON()); } catch (JSONException e) { log.error("Unexpected error while getting JSON", e); } } } for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { c.sendMessage(getUndoMessage(changeList, deleteList).toString()); } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } } else { Background previousBackground = undoSnapshots_Background.pollLast(); background = previousBackground; whiteboardMap.get(whiteboardObjectId).setBackground(previousBackground); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()) .getConnectionRegistry(); for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { if (previousBackground != null) { c.sendMessage(getAddBackgroundMessage(previousBackground.getJSON()).toString()); } else { c.sendMessage(getAddBackgroundMessage(new JSONObject()).toString()); } } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } } return true; } return false; }
From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java
License:Apache License
/** * Synchronizing eraseAll request between whiteboard clients * // w ww .j av a2s. com * @return */ private boolean handleEraseAll() { elementMap.clear(); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()).getConnectionRegistry(); for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { JSONArray jsonArray = new JSONArray(); c.sendMessage(getEraseAllMessage(jsonArray).toString()); return true; } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } return false; }
From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java
License:Apache License
/** * Load the clipArts list from the clipArt folder and synchronizing the list between whiteboard clients *//*from w w w. j a v a 2s. c o m*/ private void handleClipArts() { loadClipArts(); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()).getConnectionRegistry(); for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { JSONArray jsonArray = new JSONArray(); for (String clipArtURL : clipArts) { jsonArray.put(clipArtURL); } c.sendMessage(getClipArtListMessage(jsonArray).toString()); } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } }
From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java
License:Apache License
/** * Load the documents list from the documents folder and synchronizing the list between whiteboard clients *///from ww w. j ava 2 s . c o m private void handleDocs() { loadDocuments(); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()).getConnectionRegistry(); for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { JSONArray jsonArray = new JSONArray(); Set<String> keySet = docMap.keySet(); for (String key : keySet) { jsonArray.put(docMap.get(key).get(0)); } c.sendMessage(getDocumentListMessage(jsonArray).toString()); } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } }
From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java
License:Apache License
/** * Load the components of a particular document from the documents folder and synchronizing the list between * whiteboard clients//from w ww . j a v a2 s . c o m * * @param docBaseName */ private void handleDocComponents(String docBaseName) { loadDocuments(); IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()).getConnectionRegistry(); for (IWebSocketConnection c : reg.getConnections(Application.get())) { try { JSONArray jsonArray = new JSONArray(); for (String url : docMap.get(docBaseName)) { jsonArray.put(url); } c.sendMessage(getDocumentComponentListMessage(jsonArray).toString()); } catch (Exception e) { log.error("Unexpected error while sending message through the web socket", e); } } }