コード例 #1
0
  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");
    }
  }
コード例 #2
0
ファイル: Server.java プロジェクト: jtulach/tyrus
  public static void main(String[] args) {
    if (args.length < 4) {
      System.out.println(
          "Please provide: (<hostname>, <port>, <websockets root path>, <;-sep fully qualfied classnames of your bean>) in the command line");
      System.out.println("e.g. localhost 8021 /websockets/myapp myapp.Bean1;myapp.Bean2");
      System.exit(1);
    }
    Set<Class<?>> beanClasses = getClassesFromString(args[3]);
    int port = Integer.parseInt(args[1]);
    String hostname = args[0];
    String wsroot = args[2];

    Server server = new Server(hostname, port, wsroot, beanClasses);

    try {
      server.start();
      System.out.println("Press any key to stop the WebSocket server...");
      //noinspection ResultOfMethodCallIgnored
      System.in.read();
    } catch (IOException ioe) {
      System.err.println("IOException during server run");
      ioe.printStackTrace();
    } catch (DeploymentException de) {
      de.printStackTrace();
    } finally {
      server.stop();
    }
  }
コード例 #3
0
ファイル: WebSocketLauncher.java プロジェクト: addonin/akka
 public static void launchSessionEndpoint(
     ServletContext context, String path, String actorSystemKey) {
   ServerContainer container =
       (ServerContainer) context.getAttribute("javax.websocket.server.ServerContainer");
   try {
     container.addEndpoint(
         ServerEndpointConfig.Builder.create(SessionEndpoint.class, path)
             .configurator(new SessionEndpointConfigurator(actorSystemKey))
             .build());
   } catch (DeploymentException ex) {
     ex.printStackTrace();
   }
 }