示例#1
0
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_cards_now, container, false);

    initViewsAndAdapters();
    initListView();

    FrameLayout mFrameLayout = (FrameLayout) view.findViewById(R.id.now_cards_list);
    mFrameLayout.addView(getExpandableListView());

    mSocket.on(
        EVENT_INIT,
        args -> {
          UI.execute(
              () -> {
                JSONObject jsonObject = (JSONObject) args[0];

                try {
                  JSONArray jsonArray = jsonObject.getJSONArray(INIT_BEACONS_KEY);
                  for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject beacon = (JSONObject) jsonArray.get(i);
                    String identifier = beacon.getString(ID_KEY);
                    int floor = beacon.getInt(FLOOR_KEY);

                    BeaconLocationManager bm = HereAndNowApplication.getBeaconLocationManager();
                    bm.addBeacon(identifier, floor, getContext());
                  }

                  // Start the manager.
                  HereAndNowApplication.getBeaconLocationManager().resume();

                  // Initialize cards.
                  JSONArray cardsArray = jsonObject.getJSONArray(INIT_MESSAGES_KEY);
                  setupInitialView(cardsArray);
                } catch (JSONException e) {
                  e.printStackTrace();
                }
              });
        });

    mSocket.emit(EVENT_INIT); // Requests latest messages

    initTopicsAndCards(createPreBuiltTopics(), getSourceTopicModel(), getTopicListAdapter());
    filterModel();

    return view;
  }
示例#2
0
 private void initSocket() {
   try {
     IO.Options options = new IO.Options();
     // options.reconnectionAttempts = 1;
     // options.forceNew = true;
     options.reconnectionDelay = 1000;
     options.reconnectionDelayMax = 2000;
     options.timeout = 2000;
     mSocket = IO.socket(Constants.CHAT_SERVER_URL, options);
     mSocket.on(SocketEvent.CONNECT, onConnect);
     mSocket.on(SocketEvent.DISCONNECT, onDisconnect);
     mSocket.on(SocketEvent.CONNECT_ERROR, onConnectError);
     mSocket.on(SocketEvent.CONNECT_TIMEOUT, onConnectError);
     mSocket.on(SocketEvent.LOGIN, onLogin);
     mSocket.on(SocketEvent.USER_JOIN, onUserJoin);
     mSocket.on(SocketEvent.USER_LEFT, onUserLeft);
     mSocket.on(SocketEvent.TYPING, onTyping);
     mSocket.on(SocketEvent.STOP_TYPING, onStopTyping);
     mSocket.on(SocketEvent.LIST_CONTACT, onListContact);
     mSocket.on(SocketEvent.UPDATE_CONTACT, onUpdateContact);
     mSocket.on(SocketEvent.NEW_MESSAGE, onNewMessage);
     mSocket.on(SocketEvent.DELIVERY_STATUS, onDeliveryStatus);
     mSocket.connect();
   } catch (URISyntaxException e) {
     Log.e(Constants.TAG_CHAT, e.getMessage(), e);
   }
 }
示例#3
0
 public void connect(String url, final String userId) throws URISyntaxException {
   Log.w(TAG, "f**k the log");
   if (TextUtils.isEmpty(url)) {
     FSLog.w(TAG, "url is empty");
     return;
   }
   IO.Options options = new IO.Options();
   options.forceNew = true;
   socket = IO.socket(IMConfig.IP + ":3005", options);
   socket.on(
       Socket.EVENT_CONNECT,
       new Emitter.Listener() {
         @Override
         public void call(Object... args) {
           FSLog.w(TAG, "bull shit");
           socket.emit(
               "getUserId",
               userId,
               new Ack() {
                 @Override
                 public void call(Object... args) {}
               });
           if (loginCallback != null) {
             loginCallback.onSuccess();
             loginCallback = null;
           }
         }
       });
   socket.on(
       Socket.EVENT_DISCONNECT,
       new Emitter.Listener() {
         @Override
         public void call(Object... args) {
           FSLog.w(TAG, "disconnect");
         }
       });
   socket.on(
       Socket.EVENT_CONNECT_ERROR,
       new Emitter.Listener() {
         @Override
         public void call(Object... args) {
           FSLog.w(TAG, "connect error");
           if (loginCallback != null) {
             loginCallback.onFailed();
             loginCallback = null;
           }
         }
       });
   socket.on(
       Socket.EVENT_ERROR,
       new Emitter.Listener() {
         @Override
         public void call(Object... args) {
           FSLog.w(TAG, "EVENT_ERROR");
         }
       });
   socket.on(
       Socket.EVENT_CONNECT_TIMEOUT,
       new Emitter.Listener() {
         @Override
         public void call(Object... args) {
           FSLog.w(TAG, "time out");
         }
       });
   socket.on(
       "message",
       new Emitter.Listener() {
         @Override
         public void call(Object... args) {
           FSLog.w(TAG, args[0].toString());
           /*
           {
              contentStr = helloWorld;
              fromUserId = 6;
              msgContentType = 1;
              msgId = "768D2D05-046C-4E3C-B39A-04FC5A0245DE";
              sendDate = "1474811811.949114";
              serverReceiveDate = 1474811812153;
              state = 1;
              toUserId = 7;
           }
           */
         }
       });
   socket.connect();
   FSLog.w("zxc", "connect");
 }
示例#4
0
  private void configureEvents() {

    socket
        .on(
            Socket.EVENT_CONNECT,
            new Emitter.Listener() {
              @Override
              public void call(Object... args) {
                log("Connected");
              }
            })
        .on(
            EVENT_SOCKET_ID,
            new Emitter.Listener() { // called when connecting
              @Override
              public void call(Object... args) {
                JSONObject data = (JSONObject) args[0];
                try {
                  id = data.getString("id");
                  log(String.format("You connected with id %s", id));
                } catch (JSONException e) {
                  log("Error getting id", e);
                }
              }
            })
        .on(
            EVENT_PLAYER_CONNECTED,
            new Emitter.Listener() { // Called
              @Override
              public void call(Object... args) {
                JSONObject object = (JSONObject) args[0];
                try {
                  log("Another player connected");
                  object = object.getJSONObject("p");
                  screen.playerJoins(
                      object.getString("id"),
                      object.getString("locationId"),
                      ((Double) object.getDouble("x")).floatValue(),
                      ((Double) object.getDouble("y")).floatValue());
                } catch (JSONException e) {
                  log("Error getting id", e);
                }
              }
            })
        .on(
            EVENT_PLAYER_DISCONNECTED,
            new Emitter.Listener() {
              @Override
              public void call(Object... args) {
                JSONObject data = (JSONObject) args[0];
                try {
                  String id = data.getString("id");
                  screen.getOnlinePlayers().remove(id);
                  log(String.format("online player id %s", id));
                } catch (JSONException e) {
                  log("Error getting id", e);
                }
              }
            })
        .on(
            EVENT_ONLINE_PLAYERS,
            new Emitter.Listener() {
              @Override
              public void call(Object... args) {
                JSONArray objects = (JSONArray) args[0];
                try {
                  for (int i = 0; i < objects.length(); i++) {
                    screen.playerJoins(
                        objects.getJSONObject(i).getString("id"),
                        objects.getJSONObject(i).getString("locationId"),
                        ((Double) objects.getJSONObject(i).getDouble("x")).floatValue(),
                        ((Double) objects.getJSONObject(i).getDouble("y")).floatValue());
                  }
                } catch (JSONException e) {
                  log("Error getting id", e);
                }
              }
            })
        .on(
            EVENT_PLAYER_MOVED,
            new Emitter.Listener() {
              @Override
              public void call(Object... args) {
                JSONObject data = (JSONObject) args[0];
                try {
                  String playerId = data.getString("id");
                  Double x = data.getDouble("x");
                  Double y = data.getDouble("y");
                  String locationId = data.getString("l");

                  if (screen.getOnlinePlayers().get(playerId) != null) {
                    screen
                        .getOnlinePlayers()
                        .get(playerId)
                        .setPosition(x.floatValue(), y.floatValue());
                  }
                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            });
  }