相关疑难解决方法(0)

Spring 4 websocket没有STOMP,socketjs

我试图测试websocket而不使用socketjs库,我也不想添加任何stomp连接.

我正在关注stackoverflow问题的例子: 带有Sockjs和Spring 4的WebSocket,但是没有Stomp

所以没有stomp服务器,我已经成功通过socketjs库与url连接:ws:// localhost:8080/greeting/741/0tb5jpyi/websocket

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

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

我尝试了以下网址,但它们不起作用:

ws://localhost:8080/greeting/394/0d7xi9e1/websocket not worked
ws://localhost:8080/greeting/websocket not worked
ws://localhost:8080/greeting/ not worked 
Run Code Online (Sandbox Code Playgroud)

我应该用哪个URL连接?

spring stomp websocket spring-4 spring-websocket

12
推荐指数
2
解决办法
1万
查看次数

Springboot @ServerEndPoint"无法找到根WebApplicationContext."

我在使用带有@ServerEndPoint注释类的spring时遇到了麻烦

我正在使用Springboot 1.2.3,我正试图弄清楚如何拥有一个端点的单个实例

@SpringBootApplication
@EnableJpaRepositories
@EnableWebSocket
public class ApplicationServer {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationServer.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

弹簧配置:

@ConditionalOnWebApplication
@Configuration
public class WebSocketConfigurator {

    @Bean
    public ServerEndPoint serverEndpoint() {
        return new ServerEndPoint();
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
Run Code Online (Sandbox Code Playgroud)

WebSocket端点:

@ServerEndpoint(value = "/", decoders = MessageDecoder.class, 
encoders = MessageEncoder.class, configurator = SpringConfigurator.class)
public class ServerEndPoint {

    private static final Logger LOG = LoggerFactory.getLogger(ServerEndPoint.class);

    public static final Set<CommunicationObserver> OBSERVERS = Sets.newConcurrentHashSet();

    @OnMessage
    public void …
Run Code Online (Sandbox Code Playgroud)

java spring websocket spring-boot jsr356

7
推荐指数
2
解决办法
6903
查看次数

如何在没有STOMP的情况下使用原始Spring 4 WebSockets广播消息?

在这个伟大的答案/sf/answers/1901339051/有一个例子,说明如何使用没有STOMP子协议的原始Spring4 WebSockets(并且可能没有SockJS).

现在我的问题是:我如何向所有客户广播?我期望看到一个API,我可以使用类似于纯JSR 356 websockets API的API:session.getBasicRemote().sendText(messJson);

我是否需要自己保留所有WebSocketSession对象,然后调用sendMessage()每个对象?

spring websocket spring-4

7
推荐指数
2
解决办法
3757
查看次数

Spring websockets没有STOMP和SockJs,但具有消息代理和路由支持

有没有办法使用Spring提供的工具来连接webSocket而不使用SockJS和STOMP?我需要实现专有的webSocket协议,所以我现在不能使用任何一个.

我已经从以下答案中实施/破解了解决方案,但它需要重新实现我认为我不应该担心的事情.

我想我的问题归结为:有没有办法让我使用2.(下面)的优点,特别是MessageBroker,而不使用STOMP?

一点背景:

我需要做以下事情:

  • 能够在websocket处理程序的每个实例中为我的持久性提供程序注入引用
  • 在我的处理程序对象的单独实例中处理每个webSocket会话
  • 将我在一个端点上收到的消息重新传输到另一个端点上的多个客户端.
  • 通过标准的Text WebSocket连接实现我自己的协议.

到目前为止我尝试过的是:

  1. 使用javax.websocket,我可以在一个单独的实例上处理每个请求.但是,我需要遍历打开的会话并重新传输连接到正确端点的会话.另外,我不能使用Spring注入我的持久性提供程序.这是我到目前为止使用的东西,看起来太hacky.

即(伪代码)

@ServerEndpoint("/trade/{id}")
public String handleMessage(@PathParam(id) String id, webSocketSession session){
 if(id.equalsIgnoreCase("foo"){
      for(Session s: session.getOpenSessions(){
      //do things}
  } else if(id.equalsIgnoreCase("bar")){
      //do other things
  }
 }
}
Run Code Online (Sandbox Code Playgroud)
  1. 使用@MessageMapping("/trade")和使用Spring websocket实现@SendTo("/queue/position-updates").这要求我至少使用STOMP.这意味着要破坏流程中的其他应用程序,这是我目前无法做到的.此外,还有我需要实施的专有协议的问题.

即(伪代码)

@MessageMapping("/trade/{id}")
@SendTo("/trades/all")
public Greeting greeting(String message){
    //do things, return message
}
Run Code Online (Sandbox Code Playgroud)
  1. 目前,我通过实现WebSocketConfigurer类并注册端点来使用Spring websocket实现.这允许我将每个会话放在一个单独的处理程序类中并注入持久性提供程序.但是,我似乎无法获得有关Spring工作的一些好处:
    • 我不能得到的参数{id}registry.addHandler(WebsocketHandlerPool(), "/trade/{id}")WebsocketHandlerPool(),所以我有,当我创建每一个人来解析路径WebsocketHandler.理想情况下,我不想这样做.
    • 如果我不想迭代websocket消息处理程序代码中的所有会话,我必须实现自己的消息代理.对于@MessageMapping和,我没有支持(据我所知)@SendTo.

即(处理程序的伪代码:)

@Override
public void afterConnectionEstablished(WebSocketSession …
Run Code Online (Sandbox Code Playgroud)

java spring spring-websocket java-websocket

6
推荐指数
0
解决办法
1162
查看次数

如何使用 Spring Security 和 Spring Websocket 支持但没有 STOMP?

我目前正在构建一个使用 Spring 来利用 websocket 支持和安全性的 Web 应用程序。问题是,我不想使用 STOMP。现在已经有大约 4 个版本没有更新了,我不需要它。因此,我按照另一个 Stackoverflow问题的答案,使用 SockJS 但不使用 STOMP 为 Websocket 配置 Spring。

现在我想集成 Spring Security 来进行 websocket 身份验证和授权。不幸的是,该文档必须为 STOMP-websockets 配置 Spring Security。

我非常感谢任何为我的案例配置 Spring Security 的帮助。有人知道这方面的教程或示例吗?我还没有找到。

java security spring websocket sockjs

6
推荐指数
1
解决办法
4437
查看次数