Exemple #1
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);
   }
 }
  // gets user credentials from SharedPreferences
  private DataStore(Context context) {
    SharedPreferences settings = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
    username = settings.getString(USERNAME_KEY, "");
    password = settings.getString(PASSWORD_KEY, "");
    id = settings.getString(ID_KEY, "");

    mSocket.connect();
  }
Exemple #3
0
 public void connect() {
   try {
     socket = IO.socket(serverLocation);
     socket.connect();
     configureEvents();
   } catch (URISyntaxException e) {
     log("Error connecting to socket", e);
   }
 }
 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");
 }