Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 4无跺脚的网袜,袜子JS_Spring_Websocket_Stomp_Spring 4_Spring Websocket - Fatal编程技术网

Spring 4无跺脚的网袜,袜子JS

Spring 4无跺脚的网袜,袜子JS,spring,websocket,stomp,spring-4,spring-websocket,Spring,Websocket,Stomp,Spring 4,Spring Websocket,我试图在不使用socketjs库的情况下测试websocket,而且我不想添加任何stomp连接 我遵循stackoverflow问题中的示例: 因此,在没有stomp服务器的情况下,我成功地通过socketjs库连接到一个url:ws://localhost:8080/greeting/741/0tb5jpyi/websocket 现在我想删除socketjs库以允许原始websocket连接(可能是android、ios等设备) 当我删除参数:.withSockJS()时,我无法通过web

我试图在不使用socketjs库的情况下测试websocket,而且我不想添加任何stomp连接

我遵循stackoverflow问题中的示例:

因此,在没有stomp服务器的情况下,我成功地通过socketjs库连接到一个url:ws://localhost:8080/greeting/741/0tb5jpyi/websocket

现在我想删除socketjs库以允许原始websocket连接(可能是android、ios等设备)

当我删除参数:.withSockJS()时,我无法通过websocket进行连接

我尝试了以下URL,但不起作用:

ws://localhost:8080/greeting/394/0d7xi9e1/websocket not worked
ws://localhost:8080/greeting/websocket not worked
ws://localhost:8080/greeting/ not worked 

我应该使用哪个URL进行连接?

我在项目中使用WebSocket而没有跺脚

以下配置适用于
spring boot

在pom.xml中添加spring引导websocket依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
最后,您可以编写您的WebsocketHandler。Spring为WebSocketHandler提供了不同的抽象类(在主包中:
org.springframework.web.socket.handler
)。我的websocket配置时没有
STOMP
,我的客户端也没有使用
socket.js
。因此,
MyWebSocketHandler
扩展了TextWebSocketHandler,并覆盖了错误、打开和关闭连接以及接收到的文本的方法

@Component
public class MyWebSocketHandler extends TextWebSocketHandler {
    ...

    @Override
    public void handleTransportError(WebSocketSession session, Throwable throwable) throws Exception {
        LOG.error("error occured at sender " + session, throwable);
        ...
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        LOG.info(String.format("Session %s closed because of %s", session.getId(), status.getReason()));

        ...
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        LOG.info("Connected ... " + session.getId());

        ...
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage jsonTextMessage) throws Exception {
        LOG.debug("message received: " + jsonTextMessage.getPayload());
        ...
    }
}

您应该使用
ws://localhost:8080/greeting

new WebSocket('ws://localhost:8080/greeting')

感谢您的回复,duffy356,我有相同的配置。我正在尝试通过chrome高级rest客户端插件测试我的连接。在我以前的项目中,我能够通过该插件连接websocket。但是这个项目不是基于spring的,现在我无法通过url:ws://localhost:8080/greeting/连接websocket。我找不到正确的url进行连接。你知道吗?chrome高级rest客户端插件支持WebSocket吗?试着从chrome控制台连接到你的服务器。@duffy356谢谢你的回答。有没有可能通过ssl(wss)让WebSocket在没有STOMP的情况下工作?我可以使用什么工具来测试它?我使用了WebSocket客户端,但什么也没发生,只得到了:解析HTTP请求头java.lang.IllegalArgumentException时出错:在方法名称中找到无效字符。HTTP方法名称必须是令牌。如果使用javascript,但使用高级rest客户端,我收到了403禁止错误。默认行为是只接受相同来源的请求,这在使用高级rest客户端时可能会给您403。在端点上配置允许的来源:registry.addHandler(greetingHandler(),“/greeting”).setAllowedOrigins(“*”);如何使用安全性配置WebSocketConfigurer,我的意思是使用诸如new WebSocket之类的东西wss://localhost:8080/greeting'). 我不想使用stomp/sockjs,因为客户端是第三方,我没有控制权,唯一的选项是纯文本WebSocketHandler forme@Chakri为了配置具有安全性的websocket,您只需在属性
server.ssl.enabled=true
中激活ssl模式即可在链接:您如何管理tomcat的升级请求,该请求指示“将协议从http更新为ws”
new WebSocket('ws://localhost:8080/greeting')