java Mock sockt链接,Spring Boot WebSocket在SockJS客户端测试中抛出“拒绝连接”

本文档描述了在SpringBoot项目中集成WebSocket并进行测试时遇到的"拒绝连接"错误。作者配置了WebSocketConfig,并在测试中使用SockJS客户端尝试连接到WebSocket端点,但收到"Connection refused"的错误。问题可能在于WebSocket端点的配置或测试代码的URI。解决方案可能涉及检查服务器端口设置、网络连接和WebSocket配置的正确性。
摘要由CSDN通过智能技术生成

我正在开发Spring Boot Server项目,该项目提供了简单的REST资源,直到现在。为了将通知推送到客户端,我想添加一个websocket连接。为了测试这方面,我已经使用基于本教程SockJS客户端写的集成测试:Spring Boot WebSocket在SockJS客户端测试中抛出“拒绝连接”

问题是连接与以下错误拒绝:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:9090/websocket/info": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

我的WebSocket配置如下:

import org.springframework.context.annotation.Configuration;

import org.springframework.messaging.simp.config.MessageBrokerRegistry;

import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;

import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;

import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration

@EnableWebSocketMessageBroker

public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override

public void configureMessageBroker(MessageBrokerRegistry config) {

config.enableSimpleBroker("/topic");

config.setApplicationDestinationPrefixes("/app");

}

@Override

public void registerStompEndpoints(StompEndpointRegistry registry) {

registry

.addEndpoint("/websocket")

.setAllowedOrigins("*")

.withSockJS();

}

}

我可以看到,在套接字端点映射到日志:

2017-07-14 15:22:59.561 INFO 13765 --- [ main] o.s.w.s.s.s.WebSocketHandlerMapping : Mapped URL path [/websocket/**] onto handler of type [class org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler]

服务器端口设置为9090在application.yml文件:

server:

port: 9090

下面的单元测试是无法连接到插座:

import org.junit.Assert;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.annotation.DirtiesContext;

import org.springframework.test.context.ActiveProfiles;

import org.springframework.test.context.junit4.SpringRunner;

import org.springframework.web.socket.client.standard.StandardWebSocketClient;

import org.springframework.web.socket.messaging.WebSocketStompClient;

import org.springframework.web.socket.sockjs.client.SockJsClient;

import org.springframework.web.socket.sockjs.client.WebSocketTransport;

import org.springframework.messaging.simp.stomp.StompFrameHandler;

import org.springframework.messaging.simp.stomp.StompHeaders;

import org.springframework.messaging.simp.stomp.StompSession;

import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;

import java.lang.reflect.Type;

import java.util.concurrent.BlockingQueue;

import java.util.concurrent.LinkedBlockingDeque;

import static java.util.Arrays.asList;

import static java.util.concurrent.TimeUnit.SECONDS;

@RunWith(SpringRunner.class)

@SpringBootTest

@ActiveProfiles("test")

//@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)

public class WebSocketConnectionTest {

static final String WEBSOCKET_URI = "ws://localhost:9090/websocket";

static final String WEBSOCKET_TOPIC = "/topic";

BlockingQueue blockingQueue;

WebSocketStompClient stompClient;

@Before

public void setup() {

blockingQueue = new LinkedBlockingDeque<>();

stompClient = new WebSocketStompClient(new SockJsClient(

asList(new WebSocketTransport(new StandardWebSocketClient()))));

System.out.println(WEBSOCKET_URI);

}

@Test

public void shouldReceiveAMessageFromTheServer() throws Exception {

StompSession session = stompClient

.connect(WEBSOCKET_URI, new StompSessionHandlerAdapter() {})

.get(1, SECONDS);

session.subscribe(WEBSOCKET_TOPIC, new DefaultStompFrameHandler());

String message = "MESSAGE TEST";

session.send(WEBSOCKET_TOPIC, message.getBytes());

Assert.assertEquals(message, blockingQueue.poll(1, SECONDS));

}

class DefaultStompFrameHandler implements StompFrameHandler {

@Override

public Type getPayloadType(StompHeaders stompHeaders) {

return byte[].class;

}

@Override

public void handleFrame(StompHeaders stompHeaders, Object o) {

blockingQueue.offer(new String((byte[]) o));

}

}

}

连接被拒绝。我相当肯定,这是因为URI端点不存在,但我不知道为什么。有人知道URI中是否有错误,或者是否有其他内容导致拒绝连接?

+0

您是否从Sock js调用端点? –

+0

我在失败的集成测试中使用SockJS Java Client调用端点。这是你要求的吗? –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值