标签: spring-websocket

Spring 4 Web 套接字 - 我必须有一个 stomp 代理吗?

我一直在使用以下链接来使用新的 spring 4 websockets:

http://spring.io/guides/gs/messaging-stomp-websocket/

我想知道我是否必须使用 stomp 代理才能使用 spring 框架?有没有没有经纪人的方法来使用它?

谢谢

java spring stomp websocket spring-websocket

4
推荐指数
1
解决办法
4840
查看次数

StompFrameHandler 没有从消息中获取有效载荷

最后,我的 websocket 客户端连接到端点,但我无法提取消息有效负载。我可以获得标头,但无法识别有效载荷。

我的 WebSocket 客户端如下所示:

    WebSocketTransport webSocketTransport = new WebSocketTransport(standardWebSocketClient);
    SockJsClient sockJsClient = new SockJsClient(Arrays.asList(webSocketTransport));
    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);

    stompClient.setMessageConverter(new StringMessageConverter());

    StompSessionHandler sessionHandler = new MyStompSessionHandler();
    ListenableFuture<StompSession> connect = stompClient.connect(URL, sessionHandler);

        StompSession stompSession = connect.get();
        System.out.println("sessionId: " + stompSession.getSessionId());

        String path = "/queue/orders";

        stompSession.subscribe(path, new MySimpleStompFrameHandler());
Run Code Online (Sandbox Code Playgroud)

Stomp 帧处理程序:

    private class MySimpleStompFrameHandler implements StompFrameHandler {

    @Override
    public Type getPayloadType(StompHeaders stompHeaders) {
        System.out.println("Headers " + stompHeaders.toString());
        return String.class;
    }

    @Override
    public void handleFrame(StompHeaders stompHeaders, Object payload) {
        System.out.println("Msg " + …
Run Code Online (Sandbox Code Playgroud)

java websocket spring-websocket

4
推荐指数
1
解决办法
3809
查看次数

如何在 Spring Reactive Websocket API 中广播消息?

我正在开发一个简单的聊天。我真的很喜欢基于 Tomcat 的 spring websocket API,但是有一个问题。Tomcat 使用基于线程的模型。如果我有大约 500 个在线用户,tomcat 会启动 200 个线程(默认值)并且我的 CPU 会死掉。因此,我需要一个基于事件循环的 websocket 实现。我决定尝试使用 websockets 的全新 spring 反应式 API。

所以,我需要与聊天室聊天。当某个用户在聊天室内发送消息时,必须发送给该聊天室内的所有用户。

Spring 提供以下 API:

@Service
public class ChatWebSocketHandler implements WebSocketHandler {

    @Override
    public Mono<Void> handle(WebSocketSession session) {
        Mono<Void> input =  ;
        Mono<Void> output = ;

        return Mono.zip(input, output).then();
    }
}
Run Code Online (Sandbox Code Playgroud)

没有用于检测connecteddisconnected事件的API (就像 Tomcat 一样)。因此,我无法存储active sessions可用于消息广播的 集合。

java spring reactive-programming spring-websocket spring-webflux

4
推荐指数
1
解决办法
2769
查看次数

WebSocket 客户端无法使用不同端口连接到 Java spring WebSocket 服务器

我正在使用 Java Spring WebSocket 创建一个 WebSocket 服务器,如果从同一服务器创建 WebSocket 客户端,它实际上可以工作。

这是我的服务器端代码,非常简单

// WebSocketConfig.java
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer  {
    @Autowired
    SocketTextHandler socketTextHandler;

    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(socketTextHandler, "/sockets");
    }
    
}
Run Code Online (Sandbox Code Playgroud)
// SocketTextHandler.java
@Component
public class SocketTextHandler extends TextWebSocketHandler {
    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message)
            throws InterruptedException, IOException {
        String payload = message.getPayload();
        System.out.println("16: " + session.getId());
        session.sendMessage(new TextMessage("Resp: " + payload));
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) {
        System.out.println("Server transport error: " + exception.getMessage()); …
Run Code Online (Sandbox Code Playgroud)

java websocket spring-boot spring-websocket

4
推荐指数
1
解决办法
1408
查看次数

spring websockets:向脱机用户发送消息 - 消息代理中未排队的消息

我有一个使用消息代理(activemq)的带弹簧和websockets的webapp.

这是我的配置类:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableStompBrokerRelay("/topic","/queue/");
    config.setApplicationDestinationPrefixes("/app");
 }

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/hello").withSockJS();
 }

}
Run Code Online (Sandbox Code Playgroud)

我有一个计划的任务,不断将消息推送到用户名"StanTheMan":

@Scheduled(fixedDelay = 1000)
public void sendGreetings() {
   HelloMessage hello = new HelloMessage();
   hello.setContent("timeStamp:" + System.currentTimeMillis());
   String queueMapping = "/queue/greetings";    
   template.convertAndSendToUser(
                     "DannyD",
                     queueMapping,
                     hello);

}
Run Code Online (Sandbox Code Playgroud)

目前,如果用户没有通过websocket连接到服务器 - 他的所有消息都没有为他安排,他们只是丢弃了.每当他连接-新鲜的消息被入队他.我可以以任何方式将"convertAndSendToUser"消息"转发给离线用户"吗?我希望将已过期时间的邮件排入队列,以便以后再次连接并且过期时间未结束时可以推送脱机用户.

我怎么能实现这一目标?显然使用真正的消息代理(activemq)应该有助于实现这一点,但如何?

谢谢!

java spring activemq-classic websocket spring-websocket

3
推荐指数
1
解决办法
2520
查看次数

Spring Stomp @SubscribeMapping用户目的地

当用户使用@SubscribeMapping注释订阅stomp用户目标时,我试图收到通知.我的意思是在加入时发送一些初始化数据.

虽然我无法让这个工作:

使用Javascript:

stompClient.subscribe('/user/monitor', function(msg) {
            console.log(msg);    
        });
Run Code Online (Sandbox Code Playgroud)

Java方面:

@SubscribeMapping("/monitor-user{clientId}")
public void init(@DestinationVariable("clientId") String clientId) {
    messagingTemplate.convertAndSend("/user/" + clientId + "/monitor", getOps());
}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多映射组合,例如"/ user/monitor- {clientId}","/ user/monitor"(删除clientId),根本没有成功.

什么是预期的映射值,所以这个被调用?

谢谢!

stomp spring-websocket

3
推荐指数
1
解决办法
6376
查看次数

Spring 4.2.0 - 如何使用SimpUserRegistry

根据Spring 4.2.0文档,项目5.5,我正在尝试使用SimpUserRegistry来获取连接到websockets/STOMP端点的用户列表......但是我在Spring上很新,我只是不知道在哪里/如何使用这个类.你能给我一个例子或指出我正确的方向吗?

spring spring-mvc spring-websocket

3
推荐指数
1
解决办法
1639
查看次数

在Spring + Sockjs应用程序上手动设置传输类型

我需要在非JEE7兼容服务器上安装应用程序.我正在使用Spring + Stomp + SocksJs进行实时通知.

我的代码看起来像这样:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry ser) {
        ser.addEndpoint("/notifications").withSockJS()
    }

   }

}
Run Code Online (Sandbox Code Playgroud)

在客户端:

function setSocket(broker, callbackFn) {

    var socket = {};

    socket.cliente = new SockJS(path + broker);
    socket.stomp = Stomp.over(socket.cliente);

    socket.stomp.connect({}, function () {
        socket.stomp.subscribe("/topic" + broker, callbackFn);
    });

}
Run Code Online (Sandbox Code Playgroud)

有没有办法手动设置使用的传输类型并避免使用websockets?

java spring long-polling sockjs spring-websocket

3
推荐指数
1
解决办法
4769
查看次数

Spring WebSocket.访问Config中的Cookie

我正在尝试使用STOMP,OAuth 2和SockJS通过Spring配置WebSocket.新规范告诉我们如何使用Interceptor实现它.案例是:如果用户通过身份验证,则CONNECT请求的Native头中有一个Bearer Token,并且通过Token设置主体没有问题.但我的任务是将BrowserToken用于未经授权的用户(保存在Cookies中).我怎么能从请求中得到它?

spring-websocket

3
推荐指数
1
解决办法
1947
查看次数

Spring + Oauth2 + JWT + Websocket

我正在做一个关于Spring Boot + oauth2 + websocket的例子。我在8098端口上运行的资源服务器上具有spring websocket配置和其他代码。我试图从运行在8080端口上的客户端应用程序连接它。但是,当我运行我的应用程序时,在浏览器控制台上收到错误401(未经授权)。我正在分享一些代码:

1)客户端应用程序(在8080端口上运行)的javaScript代码获取websocket连接

'use strict';

// pull in the SockJS JavaScript library for talking over WebSockets.
var SockJS = require('sockjs-client');
// pull in the stomp-websocket JavaScript library to use the STOMP sub-protocol.
require('stompjs');

function register(registrations) {
    const access_token = localStorage.getItem("ACCESS_TOKEN");
    console.log("Access Token " + access_token);
    const csrf_token = localStorage.getItem("XSRF");
    // Here is where the WebSocket is pointed at the server application’s /messages endpoint
    // websocket use this URL to open TCP connection
    var socket = …
Run Code Online (Sandbox Code Playgroud)

spring-security jwt reactjs spring-security-oauth2 spring-websocket

3
推荐指数
1
解决办法
2590
查看次数