@Override public void onMessage(String message) { LOGGER.debug("receiving from websocket " + message); if (message.contains("{\"type\":\"pong\",\"reply_to\"")) { int rightBracketIdx = message.indexOf('}'); String toParse = message.substring(26, rightBracketIdx); lastPingAck = Integer.parseInt(toParse); LOGGER.debug("pong received " + lastPingAck); } else { JSONObject object = parseObject(message); SlackEvent slackEvent = SlackJSONMessageParser.decode(this, object); if (slackEvent instanceof SlackChannelCreated) { SlackChannelCreated slackChannelCreated = (SlackChannelCreated) slackEvent; channels.put( slackChannelCreated.getSlackChannel().getId(), slackChannelCreated.getSlackChannel()); } if (slackEvent instanceof SlackGroupJoined) { SlackGroupJoined slackGroupJoined = (SlackGroupJoined) slackEvent; channels.put( slackGroupJoined.getSlackChannel().getId(), slackGroupJoined.getSlackChannel()); } if (slackEvent instanceof SlackUserChange) { SlackUserChange slackUserChange = (SlackUserChange) slackEvent; users.put(slackUserChange.getUser().getId(), slackUserChange.getUser()); } dispatcher.dispatch(slackEvent); } }
private void connectImpl() throws IOException, ClientProtocolException, ConnectException { LOGGER.info("connecting to slack"); lastPingSent = 0; lastPingAck = 0; HttpClient httpClient = getHttpClient(); HttpGet request = new HttpGet(SLACK_HTTPS_AUTH_URL + authToken); HttpResponse response; response = httpClient.execute(request); LOGGER.debug(response.getStatusLine().toString()); String jsonResponse = CharStreams.toString(new InputStreamReader(response.getEntity().getContent())); SlackJSONSessionStatusParser sessionParser = new SlackJSONSessionStatusParser(jsonResponse); try { sessionParser.parse(); } catch (ParseException e1) { LOGGER.error(e1.toString()); } if (sessionParser.getError() != null) { LOGGER.error("Error during authentication : " + sessionParser.getError()); throw new ConnectException(sessionParser.getError()); } users = sessionParser.getUsers(); channels = sessionParser.getChannels(); sessionPersona = sessionParser.getSessionPersona(); team = sessionParser.getTeam(); LOGGER.info("Team " + team.getId() + " : " + team.getName()); LOGGER.info("Self " + sessionPersona.getId() + " : " + sessionPersona.getUserName()); LOGGER.info(users.size() + " users found on this session"); LOGGER.info(channels.size() + " channels found on this session"); String wssurl = sessionParser.getWebSocketURL(); LOGGER.debug("retrieved websocket URL : " + wssurl); ClientManager client = ClientManager.createClient(); if (proxyAddress != null) { client .getProperties() .put(ClientProperties.PROXY_URI, "http://" + proxyAddress + ":" + proxyPort); } final MessageHandler handler = this; LOGGER.debug("initiating connection to websocket"); try { websocketSession = client.connectToServer( new Endpoint() { @Override public void onOpen(Session session, EndpointConfig config) { session.addMessageHandler(handler); } }, URI.create(wssurl)); } catch (DeploymentException e) { LOGGER.error(e.toString()); } if (websocketSession != null) { SlackConnectedImpl slackConnectedImpl = new SlackConnectedImpl(sessionPersona); dispatcher.dispatch(slackConnectedImpl); LOGGER.debug("websocket connection established"); LOGGER.info("slack session ready"); } }