private static ServerEvent parseJsonEvent(String msg) { try { JSONObject eventJ = JSONParser.parseStrict(msg).isObject(); Name name = new Name(eventJ.get("name").isString().stringValue(), ""); ServerEvent.Scope scope = ServerEvent.Scope.valueOf(eventJ.get("scope").isString().stringValue()); ServerEvent.DataType dataType = eventJ.get("dataType") == null ? ServerEvent.DataType.STRING : ServerEvent.DataType.valueOf(eventJ.get("dataType").isString().stringValue()); Serializable data; String from = eventJ.get("from") == null ? null : eventJ.get("from").toString(); if (dataType == ServerEvent.DataType.BG_STATUS) { data = BackgroundStatus.parse(eventJ.get("data").isString().stringValue()); } else if (dataType == ServerEvent.DataType.JSON) { data = eventJ.get("data").isObject().toString(); } else { data = eventJ.get("data").isString().stringValue(); } ServerEvent sEvent = new ServerEvent(name, scope, dataType, data); sEvent.setFrom(from); return sEvent; } catch (Exception e) { GwtUtil.getClientLogger() .log(Level.WARNING, "Unable to parse json message into ServerEvent: " + msg, e); return null; } }
public static void onMessage(String msg) { try { GwtUtil.getClientLogger().log(Level.INFO, "onMessage: " + msg); ServerEvent sEvent = parseJsonEvent(msg); Name name = sEvent == null ? null : sEvent.getName(); Serializable data = sEvent.getData(); if (name == null) { GwtUtil.getClientLogger().log(Level.INFO, "Failed to evaluate: " + msg); } if (name.equals(Name.EVT_CONN_EST)) { JSONObject dataJ = JSONParser.parseStrict(sEvent.getData().toString()).isObject(); String sEventInfo = dataJ.get("connID").isString().stringValue() + "_" + dataJ.get("channel").isString().stringValue(); Cookies.setCookie("seinfo", sEventInfo); GwtUtil.getClientLogger() .log(Level.INFO, "Websocket connection established: " + sEventInfo); } else if (data instanceof BackgroundStatus) { WebEvent<String> ev = new WebEvent<String>( ClientEventQueue.class, name, ((BackgroundStatus) data).serialize()); WebEventManager.getAppEvManager().fireEvent(ev); GwtUtil.getClientLogger() .log(Level.INFO, "Event: Name:" + name.getName() + ", Data: " + ev.getData()); } else { WebEvent<String> ev = new WebEvent<String>(ClientEventQueue.class, name, String.valueOf(data)); WebEventManager.getAppEvManager().fireEvent(ev); GwtUtil.getClientLogger() .log(Level.INFO, "Event: Name:" + name.getName() + ", Data: " + sEvent.getData()); } } catch (Exception e) { GwtUtil.getClientLogger() .log(Level.WARNING, "Exception interpreting incoming message: " + msg, e); } }
public static void sendEvent(ServerEvent sevt) { sendMessage(sevt.toJsonString()); }