相关疑难解决方法(0)

在Spring Websocket上向特定用户发送消息

如何仅从服务器向特定用户发送websocket消息?

我的webapp具有弹簧安全设置并使用websocket.我遇到了一个棘手的问题,试图从服务器向特定用户发送消息.

我阅读本手册的理解来自我们可以做的服务器

simpMessagingTemplate.convertAndSend("/user/{username}/reply", reply);
Run Code Online (Sandbox Code Playgroud)

在客户端:

stompClient.subscribe('/user/reply', handler);
Run Code Online (Sandbox Code Playgroud)

但我永远无法调用订阅回调.我尝试了许多不同的路径,但没有运气.

如果我将它发送到/ topic/reply它可以工作,但所有其他连接的用户也会收到它.

为了说明问题,我在github上创建了这个小项目:https://github.com/gerrytan/wsproblem

重现步骤:

1)克隆并构建项目(确保您使用的是jdk 1.7和maven 3.1)

$ git clone https://github.com/gerrytan/wsproblem.git
$ cd wsproblem
$ mvn jetty:run
Run Code Online (Sandbox Code Playgroud)

2)导航到http://localhost:8080,使用bob/test或jim/test登录

3)单击"请求用户特定消息".预期:仅对此用户的"仅收到消息给我"旁边显示消息"hello {username}",实际:未收到任何消息

java spring spring-mvc spring-websocket

72
推荐指数
2
解决办法
5万
查看次数

Web应用程序似乎已启动名为[Timer-0]的线程但未能将其停止

我正在使用springboot 1.5.9.RELEASE + Java 8 + tomcat 9 + Jersey + Oracle和我的应用程序已安排的方法定义如下:

@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }

    @Bean(destroyMethod = "shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    }
}
Run Code Online (Sandbox Code Playgroud)

工作班:

@Component
public class ClearCacheJob {



    @Scheduled(fixedRate = 3600000, initialDelay = 10000)
    public void clearErrorCodesCache() {
        try {
            logger.info("######## ClearCacheJob #########");
        } catch (Exception e) {
            logger.error("Exception in ClearCacheJob", e);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我也有一个类来取消注册oracle驱动程序,如下所示:

@WebListener
public class ContainerContextClosedHandler implements …
Run Code Online (Sandbox Code Playgroud)

java multithreading tomcat spring-boot

23
推荐指数
2
解决办法
3万
查看次数

在Spring中使用STOMP和WebSocket向特定用户发送消息时检查身份验证

我正在开发一个实时通知系统弹簧4使用内置的Message Broker中,和的WebSocket.

我希望能够根据用户名向特定用户发送消息.为了实现这个目标,我正在使用类的convertAndSendToUser方法org.springframework.messaging.simp.SimpMessagingTemplate,如下所示:

private final MessagingTemplate messagingTemplate;

@Autowired
public LRTStatusListener(SimpMessagingTemplate messagingTemplate) {
    this.messagingTemplate = messagingTemplate;
}


@Scheduled(fixedDelay=5000)
public void sendMessages(Principal principal)
    messagingTemplate
        .convertAndSendToUser(principal.getName(), "/horray", "Horray, " + principal.getName() + "!");
}
Run Code Online (Sandbox Code Playgroud)

作为配置:

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

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

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic", "/queue", "/user");
    }

}
Run Code Online (Sandbox Code Playgroud)

客户端(通过JavaScript),我应该通过指定用户名来订阅一个频道(根据另一个非常类似的问题:在Spring Websocket上向特定用户发送消息).

stompClient.subscribe('/user/' + …
Run Code Online (Sandbox Code Playgroud)

spring stomp websocket spring-messaging

8
推荐指数
2
解决办法
8105
查看次数