gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:53:54 30 4
gpt4 key购买 nike

我需要帮助在 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 连接已关闭”。Web服务器位于Heroku云系统上。这是我来自 android Activity 的连接代码:

    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方面,我使用的URL以“ws://”开头,以“websocket”结尾,例如“ws://”+ SERVER_URL +“/websocket”

请参阅服务器端的此答案 https://stackoverflow.com/a/41751897/5392825

关于java - 连接到 StompClient 时我可能在哪里出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43394174/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com