连接到 StompClient 时我可能在哪里出错?

Posted

技术标签:

【中文标题】连接到 StompClient 时我可能在哪里出错?【英文标题】:Where I may wrong while connected to StompClient? 【发布时间】:2017-04-13 13:53:30 【问题描述】:

我需要有关在 android 上连接到基于 Spring boot 的 WebSocket 服务器的帮助。这台服务器的源码我拍了https://spring.io/guides/gs/messaging-stomp-websocket/

此示例在服务器和浏览器客户端上一切正常, 但是如果我使用 StompClient (https://github.com/NaikSoftware/StompProtocolAndroid) 连接我的套接字,我会得到 mStompClient.isConnected() == false 并且 conand mStompClient.send(...) 不会发送任何东西 (?)。

几分钟后,socketweb 服务器关闭了连接,我进入了我的日志:'~~ Stomp connection closed'。 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");
        
    

这将是我的错误,但如果我使用 spring boot WebSocketStompClient 连接到我的服务器套接字,一切都可以正常工作:

    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);
        

有什么想法吗?非常感谢!

【问题讨论】:

我使用相同的库来连接基于 Stomp 的 Web 套接字。服务器端有一些配置。在 android 端,我使用以“ws://”开头并以“websocket”结尾的 URL,例如“ws://”+ SERVER_URL +“/websocket”。请参阅服务器端的此答案:***.com/a/41751897/5392825 你完全正确!它解决了我的问题。 如果我的回答解决了您的问题,请接受并投票赞成。 【参考方案1】:

我使用相同的库来连接基于 Stomp 的 Web 套接字。服务器端有一些配置。在 android 端,我使用以 "ws://" 开头并以 "websocket" 结尾的 URL,例如 "ws://" + SERVER_URL + "/websocket"

查看服务器端https://***.com/a/41751897/5392825的这个答案

【讨论】:

以上是关于连接到 StompClient 时我可能在哪里出错?的主要内容,如果未能解决你的问题,请参考以下文章

连接到Mlab时出错

使用 StompClient 在 Spring WebSocket 上传递消息时出错

使用 PGAdmin 连接到 Heroku DB 时出错

无法连接到虚拟机怎么解决?怎么解决?

连接字符串出错,如何将 Prisma 连接到 Azure SQL 数据库

连接到 phpmyadmin 时出错