2015-02-18 1 views
1

После нескольких попыток, я не могу получать все сообщения SignalR в приложении для Android. На мой взгляд, это похоже на то, что связано с LongPollingTransport, но я не могу найти решение ... Я получаю все сообщения longPolling, но иногда он входит в обработчик, а в других случаях он пытается снова подключиться к серверу без ввода метод обработчика.Android SignalR java-client: не получает все сообщения SignalR на HubConnection

Я попытался следовать примерам на here.

Код очень прост:

public void startNewSignalR(){ 

    Platform.loadPlatformComponent(new AndroidPlatformComponent()); 

    // Create a new console logger 
    Logger logger = new Logger() { 
     @Override 
     public void log(String message, LogLevel level) { 
      System.out.println(message); 
     } 
    }; 

    Uri signalRSource = Uri.parse(serverUrl); 
    HubConnection connection = new HubConnection(signalRSource.toString(), "", true, logger); 
    eventHub = connection.createHubProxy("eventHub"); 

    connection.error(new ErrorCallback() { 

     @Override 
     public void onError(Throwable error) { 
      System.err.println("There was an error communicating with the server."); 
      System.err.println("Error detail: " + error.toString()); 

      error.printStackTrace(System.err); 
     } 
    }); 

    // Subscribe to the connected event 
    connection.connected(new Runnable() { 

     @Override 
     public void run() { 
      System.out.println("CONNECTED"); 
     } 
    }); 

    // Subscribe to the closed event 
    connection.closed(new Runnable() { 

     @Override 
     public void run() { 
      System.out.println("DISCONNECTED"); 
     } 
    }); 

    eventHub.subscribe(new Object() { 
     @SuppressWarnings("unused") 
     public void receivePendingCountUpdate(String A) { 
      System.out.println("------------------------>>>>> Count: " + A); 
     } 
    }); 

    // Start the connection 
    connection.start(new LongPollingTransport(logger)) 
      .done(new Action<Void>() { 

       @Override 
       public void run(Void obj) throws Exception { 
        System.out.println("Done Connecting!"); 
        eventHub.invoke("JoinGroup", "signalevents-" + userId + "-pendingcount"); 
       } 
      }); 
} 

И здесь вы можете увидеть консольный вывод, что я получаю, когда первое сообщение не входит метод, а второй, который входит:

02-18 16:23:08.954 6877-9048/com.sensingcontrol.android I/System.out﹕ HubConnection - Starting transport for Reconnection 
02-18 16:23:10.307 6877-9031/com.sensingcontrol.android I/System.out﹕ longPolling - Trigger onData with data: {"C":"s-0,10BC5E0|1,264DB67|2,445369|3,106F98E|4,1EAF347","M":[{"H":"EventHub","M":"receivePendingCountUpdate","A":[208]}]} 
02-18 16:23:10.307 6877-9031/com.sensingcontrol.android I/System.out﹕ HubConnection - Received data: 
02-18 16:23:10.307 6877-9031/com.sensingcontrol.android I/System.out﹕ MessageId received: s-0,10BC5E0|1,264DB67|2,445369|3,106F98E|4,1EAF347 
02-18 16:23:10.307 6877-9031/com.sensingcontrol.android I/System.out﹕ Invoking OnReceived with: null 
02-18 16:23:10.307 6877-9031/com.sensingcontrol.android I/System.out﹕ HubConnection - Processing message 
02-18 16:23:10.313 6877-9048/com.sensingcontrol.android I/System.out﹕ longPolling - Start the communication with the server 
02-18 16:23:10.317 6877-9048/com.sensingcontrol.android I/System.out﹕ HubConnection - Getting connection data: [{"name":"eventhub"}] 
02-18 16:23:10.317 6877-9048/com.sensingcontrol.android I/System.out﹕ longPolling - Execute the request 
02-18 16:23:10.318 6877-9048/com.sensingcontrol.android I/System.out﹕ Create new thread for HTTP Connection 
02-18 16:23:10.337 6877-9061/com.sensingcontrol.android I/System.out﹕ Execute the HTTP Request 
02-18 16:23:10.337 6877-9061/com.sensingcontrol.android I/System.out﹕ URL: http://url/signalr/reconnect 
02-18 16:23:10.342 6877-9061/com.sensingcontrol.android I/System.out﹕ VERB: GET 
02-18 16:23:10.342 6877-9061/com.sensingcontrol.android I/System.out﹕ Header User-Agent: SignalR (lang=Java; os=android; version=2.0) 
02-18 16:23:10.342 6877-9061/com.sensingcontrol.android I/System.out﹕ CONTENT: null 
02-18 16:23:10.343 6877-9061/com.sensingcontrol.android I/System.out﹕ Request executed 
02-18 16:23:10.498 6877-9061/com.sensingcontrol.android I/System.out﹕ HubConnection - Entered startLock after transport was started 
02-18 16:23:10.498 6877-9061/com.sensingcontrol.android I/System.out﹕ HubConnection - Current state: Reconnecting 
02-18 16:23:10.498 6877-9061/com.sensingcontrol.android I/System.out﹕ HubConnection - Starting Heartbeat monitor 
02-18 16:23:10.498 6877-9061/com.sensingcontrol.android I/System.out﹕ HubConnection - Reconnected 
02-18 16:23:10.498 6877-9061/com.sensingcontrol.android I/System.out﹕ longPolling - Response received 
02-18 16:23:10.498 6877-9061/com.sensingcontrol.android I/System.out﹕ longPolling - Read response to the end 
02-18 16:23:10.918 6877-9032/com.sensingcontrol.android I/System.out﹕ longPolling - Trigger onData with data: {"C":"s-0,10BC5E1|1,264DB67|2,44536B|3,106F98F|4,1EAF347","M":[{"H":"EventHub","M":"receivePendingCountUpdate","A":[208]}]} 
02-18 16:23:10.918 6877-9032/com.sensingcontrol.android I/System.out﹕ HubConnection - Received data: 
02-18 16:23:10.938 6877-9030/com.sensingcontrol.android I/System.out﹕ MessageId received: s-0,10BC5E1|1,264DB67|2,44536B|3,106F98F|4,1EAF347 
02-18 16:23:10.938 6877-9030/com.sensingcontrol.android I/System.out﹕ Invoking OnReceived with: null 
02-18 16:23:10.938 6877-9030/com.sensingcontrol.android I/System.out﹕ HubConnection - Processing message 
02-18 16:23:10.940 6877-9047/com.sensingcontrol.android I/System.out﹕ longPolling - Start the communication with the server 
02-18 16:23:10.955 6877-9047/com.sensingcontrol.android I/System.out﹕ HubConnection - Getting connection data: [{"name":"eventhub"}] 
02-18 16:23:10.955 6877-9047/com.sensingcontrol.android I/System.out﹕ longPolling - Execute the request 
02-18 16:23:10.979 6877-9046/com.sensingcontrol.android I/System.out﹕ Create new thread for HTTP Connection 
02-18 16:23:10.989 6877-9074/com.sensingcontrol.android I/System.out﹕ Execute the HTTP Request 
02-18 16:23:10.989 6877-9074/com.sensingcontrol.android I/System.out﹕ URL: http://url/signalr/reconnect 
02-18 16:23:10.989 6877-9074/com.sensingcontrol.android I/System.out﹕ VERB: GET 
02-18 16:23:10.997 6877-9073/com.sensingcontrol.android I/System.out﹕ Header User-Agent: SignalR (lang=Java; os=android; version=2.0) 
02-18 16:23:10.997 6877-9073/com.sensingcontrol.android I/System.out﹕ CONTENT: null 
02-18 16:23:10.998 6877-9073/com.sensingcontrol.android I/System.out﹕ Request executed 
02-18 16:23:11.217 6877-9074/com.sensingcontrol.android I/System.out﹕ HubConnection - Entered startLock after transport was started 
02-18 16:23:11.217 6877-9074/com.sensingcontrol.android I/System.out﹕ HubConnection - Current state: Reconnecting 
02-18 16:23:11.218 6877-9074/com.sensingcontrol.android I/System.out﹕ HubConnection - Starting Heartbeat monitor 
02-18 16:23:11.219 6877-9074/com.sensingcontrol.android I/System.out﹕ HubConnection - Reconnected 
02-18 16:23:11.239 6877-9073/com.sensingcontrol.android I/System.out﹕ longPolling - Response received 
02-18 16:23:11.239 6877-9073/com.sensingcontrol.android I/System.out﹕ longPolling - Read response to the end 
02-18 16:23:12.721 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Timeout 
02-18 16:23:12.721 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Stopping Heartbeat monitor 
02-18 16:23:12.722 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Restarting the transport 
02-18 16:23:12.722 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Entered startLock in startTransport 
02-18 16:23:12.722 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Starting the transport 
02-18 16:23:12.722 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Stopping heartbeat monitor 
02-18 16:23:12.722 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Clearing invocation callbacks: Reconnecting 
02-18 16:23:12.723 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Starting transport for Reconnection 
02-18 16:23:14.073 6877-9061/com.sensingcontrol.android I/System.out﹕ longPolling - Trigger onData with data: {"C":"s-0,10BC5E7|1,264DB6C|2,445378|3,106F995|4,1EAF34C","M":[{"H":"EventHub","M":"receivePendingCountUpdate","A":[209]}]} 
02-18 16:23:14.073 6877-9061/com.sensingcontrol.android I/System.out﹕ HubConnection - Received data: 
02-18 16:23:14.074 6877-9061/com.sensingcontrol.android I/System.out﹕ MessageId received: s-0,10BC5E7|1,264DB6C|2,445378|3,106F995|4,1EAF34C 
02-18 16:23:14.074 6877-9061/com.sensingcontrol.android I/System.out﹕ Invoking OnReceived with: null 
02-18 16:23:14.123 6877-9073/com.sensingcontrol.android I/System.out﹕ HubConnection - Processing message 
02-18 16:23:14.123 6877-9067/com.sensingcontrol.android I/System.out﹕ longPolling - Start the communication with the server 
02-18 16:23:14.124 6877-9067/com.sensingcontrol.android I/System.out﹕ HubConnection - Getting connection data: [{"name":"eventhub"}] 
02-18 16:23:14.124 6877-9067/com.sensingcontrol.android I/System.out﹕ longPolling - Execute the request 
02-18 16:23:14.124 6877-9067/com.sensingcontrol.android I/System.out﹕ Create new thread for HTTP Connection 
02-18 16:23:14.141 6877-9078/com.sensingcontrol.android I/System.out﹕ Execute the HTTP Request 
02-18 16:23:14.141 6877-9078/com.sensingcontrol.android I/System.out﹕ URL: http://url/signalr/reconnect 
02-18 16:23:14.141 6877-9078/com.sensingcontrol.android I/System.out﹕ VERB: GET 
02-18 16:23:14.149 6877-9077/com.sensingcontrol.android I/System.out﹕ Header User-Agent: SignalR (lang=Java; os=android; version=2.0) 
02-18 16:23:14.149 6877-9077/com.sensingcontrol.android I/System.out﹕ CONTENT: null 
02-18 16:23:14.149 6877-9077/com.sensingcontrol.android I/System.out﹕ Request executed 
02-18 16:23:14.267 6877-9079/com.sensingcontrol.android I/System.out﹕ HubConnection - Entered startLock after transport was started 
02-18 16:23:14.267 6877-9079/com.sensingcontrol.android I/System.out﹕ HubConnection - Current state: Reconnecting 
02-18 16:23:14.267 6877-9079/com.sensingcontrol.android I/System.out﹕ HubConnection - Starting Heartbeat monitor 
02-18 16:23:14.268 6877-9079/com.sensingcontrol.android I/System.out﹕ HubConnection - Reconnected 
02-18 16:23:14.271 6877-9077/com.sensingcontrol.android I/System.out﹕ longPolling - Response received 
02-18 16:23:14.271 6877-9077/com.sensingcontrol.android I/System.out﹕ longPolling - Read response to the end 
02-18 16:23:16.117 6877-9077/com.sensingcontrol.android I/System.out﹕ longPolling - Trigger onData with data: {"C":"s-0,10BC5F2|1,264DB6E|2,44537C|3,106F997|4,1EAF34E","M":[{"H":"EventHub","M":"receivePendingCountUpdate","A":[209]}]} 
02-18 16:23:16.117 6877-9077/com.sensingcontrol.android I/System.out﹕ HubConnection - Received data: 
02-18 16:23:16.118 6877-9077/com.sensingcontrol.android I/System.out﹕ MessageId received: s-0,10BC5F2|1,264DB6E|2,44537C|3,106F997|4,1EAF34E 
02-18 16:23:16.136 6877-9077/com.sensingcontrol.android I/System.out﹕ Invoking OnReceived with: null 
02-18 16:23:16.137 6877-9077/com.sensingcontrol.android I/System.out﹕ HubConnection - Processing message 
02-18 16:23:16.137 6877-9077/com.sensingcontrol.android I/System.out﹕ HubConnection - Getting HubInvocation from message 
02-18 16:23:16.138 6877-9077/com.sensingcontrol.android I/System.out﹕ HubConnection - Message for: eventhub 
02-18 16:23:16.138 6877-9077/com.sensingcontrol.android I/System.out﹕ HubConnection - Invoking event: receivependingcountupdate with arguments [209] 
02-18 16:23:16.138 6877-9077/com.sensingcontrol.android I/System.out﹕ HubProxy eventHub - Handling dynamic subscription: receivePendingCountUpdate 
02-18 16:23:16.139 6877-9077/com.sensingcontrol.android I/System.out﹕ HubProxy eventHub - Invoking method for dynamic subscription: receivePendingCountUpdate 
02-18 16:23:16.173 6877-9079/com.sensingcontrol.android I/System.out﹕ ------------------------>>>>> Count: 209 
02-18 16:23:16.173 6877-9079/com.sensingcontrol.android I/System.out﹕ longPolling - Continue polling 
+0

Смотрите эту http://stackoverflow.com/a/42527962/1770868 –

ответ

6

Im специалист в SignalR андроида клиента, и я покажу вам лучший способ сделать это:

Шаг 1: настроить подключение концентратора литий ке это

private HubConnection _connection; 
private HubProxy _hub; 

String host = "http://www.thisisawebsite.net"; //The url from your web site 
      _connection = new HubConnection(host); 
      _hub = _connection.createHubProxy("NameHub"); //The name from your hub class in your server 

Шаг 2:

сделать связь с частным способом, или как вы хотите:

private SignalRFuture<Void> _awaitConnection; 

    private void startConnection(){ 
      _awaitConnection = _connection.start(new LongPollingTransport(_connection.getLogger())); 
//you can do here a while loop for reintents or something (this is a good practices, at least 3 tries. 
       try {   
        _awaitConnection.get(2000,TimeUnit.MILLISECONDS); 
        escucharGrupos(); 
        isConnected = true; 
        break; 
       } catch (InterruptedException e) { 
         System.out.println("Disconnect . . ."); 
       } catch (ExecutionException e) { 
         System.out.println("Error . . ."); 
       }catch(Exception e){} 
     } 

если все здесь правильно, вы можете напечатать что-то вроде System.out. println («Соединение сделано»); и это означает, что ваше постоянное соединение между вашим клиентом и вашим сервером уже сделано и прекрасно.

Шаг 3: Настройка обработчиков:

Я обычно делаю это с помощью метода:

private SubscriptionHandler1 handlerCon; 
private void starHandlersConnection(){ 
handlerCon = new SubscriptionHandler1<String>() { 
      @Override 
      public void run(String p1) { 
       //Here is where we get back the response from the server. Do stuffs 
      } 
     }; 

_hub.on("NameOfFunctionOfYourHub",handlerCon,String.class); 
} 

Выполните все эти шаги, и это должно работать нормально.

Также как совет, сделайте свой концентратор соединений в классе обслуживания. Таким образом вы можете делать чаты, такие как WhatsAp или телеграмму.

+0

Вы можете помочь здесь, пожалуйста: http://stackoverflow.com/questions/33895278/expandable-listview-does-not-refresh-childs -items –

+0

Я использую сигнал r в своем приложении. Но он пропускает несколько сообщений случайным образом. что может быть проблемой. – Kurkula

+0

@AbelMorgan mHubProxy.on (CLIENT_METHOD_BROADAST_MESSAGE, новый SubscriptionHandler3 () это отлично работает и вызывается при ответе от сервера, но я повторно подключил signlr-соединение, когда Интернет потерян , но метод mHubProxy.invoke - это вызов и метод, отправленный на сервер, но ответ не пришел, а не вызвал метод mHubProxy.on. Пожалуйста, помогите мне решить проблему. –

 Смежные вопросы

  • Нет связанных вопросов^_^