2017-04-13 63 views
0

我需要在android上連接到基於Spring啓動的WebSocket服務器的幫助。這個服務器我已經採取https://spring.io/guides/gs/messaging-stomp-websocket/連接到StompClient時可能出錯?

一切的源代碼工作正常,服務器和瀏覽器客戶端在這個樣本, 但如果我用StompClient(https://github.com/NaikSoftware/StompProtocolAndroid)在我的插座連接我得到mStompClient.isConnected()==假和conand mStompClient.send(...)不發送任何東西(?)。

幾分鐘後,socketweb服務器關閉連接,我得到我的日誌:'~~ Stomp連接關閉'。 Web服務器位於Heroku雲系統上。 有來自Android的活動我的連接代碼:

private StompClient mStompClient; 

    private void connectStomp(){ 
     mStompClient = Stomp.over(WebSocket.class, "wss://myserver/gs-guide-websocket"); 

     mStompClient.topic("/topic/greetings").subscribe(new Action1<StompMessage>() { 
      @Override 
      public void call(StompMessage stompMessage) { 
       Log.w(TAG, "== "+stompMessage.getPayload()); 
      } 
     }); 


     mStompClient.connect(); 

     mStompClient.lifecycle().subscribe(new Action1<LifecycleEvent>() { 
      @Override 
      public void call(LifecycleEvent lifecycleEvent) { 
       switch (lifecycleEvent.getType()) { 

        case OPENED: 
         Log.w(TAG, "~~ Stomp connection opened"); 
         break; 

        case ERROR: 
         Log.e(TAG, "~~ Error", lifecycleEvent.getException()); 
         break; 

        case CLOSED: 
         Log.w(TAG, "~~ Stomp connection closed "+lifecycleEvent.getMessage()); 
         break; 
       } 
      } 
     }); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     connectStomp(); 
    } 

    // Send test request to server 
    public void onSend(View view){ 
     Log.w(TAG,"onSend: click"); 
     mStompClient.send("/app/hello","Test").subscribe(new Observer<Void>() { 
      @Override 
      public void onCompleted() { 
       Log.w(TAG, "~~~~ onCompleted"); 
      } 

      @Override 
      public void onError(Throwable e) { 
       Log.w(TAG, "~~~~ onCompleted "+e.getMessage()); 
      } 

      @Override 
      public void onNext(Void aVoid) { 
       Log.w(TAG, "~~~~ onNext "); 
      } 
     }); 
     if (mStompClient.isConnected()){ 
      mStompClient.send("/app/hello","test msg").subscribe(); 
      Log.w("aaaa : ","onCreate: connected"); 
     } 
    } 

這將是我的錯,但如果我連接到帶彈簧引導WebSocketStompClient everithing我的服務器套接字正常工作:

private SockJsClient sockJsClient; 

    private WebSocketStompClient stompClient; 

    private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); 

    @Before 
    public void setup() { 
     List<Transport> transports = new ArrayList<>(); 
     transports.add(new WebSocketTransport(new StandardWebSocketClient())); 
     this.sockJsClient = new SockJsClient(transports); 

     this.stompClient = new WebSocketStompClient(sockJsClient); 
     this.stompClient.setMessageConverter(new MappingJackson2MessageConverter()); 
    } 

    @Test 
    public void getGreeting() throws Exception { 

     final CountDownLatch latch = new CountDownLatch(1); 
     final AtomicReference<Throwable> failure = new AtomicReference<>(); 

     StompSessionHandler handler = new TestSessionHandler(failure) { 

      @Override 
      public void afterConnected(final StompSession session, StompHeaders connectedHeaders) { 
       session.subscribe("/topic/greetings", new StompFrameHandler() { 
        @Override 
        public Type getPayloadType(StompHeaders headers) { 
         return Greeting.class; 
        } 

        @Override 
        public void handleFrame(StompHeaders headers, Object payload) { 
         Greeting greeting = (Greeting) payload; 

         try { 
          System.out.println(greeting.getContent()); 
          assertEquals("Hello, Spring!", greeting.getContent()); 
         } catch (Throwable t) { 
          System.out.println(t.getMessage()); 
          failure.set(t); 
         } finally { 
          session.disconnect(); 
          latch.countDown(); 
         } 
        } 
       }); 
       try { 
        session.send("/app/hello", "Test"); 
       } catch (Throwable t) { 
        failure.set(t); 
        latch.countDown(); 
       } 
      } 
     }; 

     this.stompClient.connect("wss://myserver/gs-guide-websocket", this.headers, handler, 443); 

     if (latch.await(10, TimeUnit.SECONDS)) { 
      if (failure.get() != null) { 
       throw new AssertionError("", failure.get()); 
      } 
     } 
     else { 
      fail("Greeting not received"); 
     } 

    } 

    private class TestSessionHandler extends StompSessionHandlerAdapter { 

     private final AtomicReference<Throwable> failure; 


     public TestSessionHandler(AtomicReference<Throwable> failure) { 
      this.failure = failure; 
     } 

     @Override 
     public void handleFrame(StompHeaders headers, Object payload) { 
      this.failure.set(new Exception(headers.toString())); 
     } 

     @Override 
     public void handleException(StompSession s, StompCommand c, StompHeaders h, byte[] p, Throwable ex) { 
      this.failure.set(ex); 
     } 

任何想法?非常感謝!

+0

我用同樣的庫,以基於網絡踐踏插座連接。服務器端有一些配置。在android方面,我使用的URL以「ws://」開始,以「websocket」結尾,如「ws://」+ SERVER_URL +「/ websocket」。看到服務器端的這個答案:http://stackoverflow.com/a/41751897/5392825 –

+0

你是絕對正確的! 它解決了我的問題。 –

+0

如果解決了您的問題,請接受我的回答並投票給我。 –

回答

1

我使用相同的庫爲了連接基於Stomp的網絡套接字。服務器端有一些配置。在android方面,我以「ws://」開始,並以「websocket」「ws://」+ SERVER_URL +「/ websocket」結尾。

看到這個答案對於服務器端:stackoverflow.com/a/41751897/5392825