- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试做什么
我有一个有玩家的大厅,当有人离开大厅时,我想为每个客户端更新它,以便显示实际的玩家列表。
我做了什么
为了避免从前端向后端发送循环请求,我决定使用网络套接字
。当有人离开大厅时,请求会发送到 REST api,然后后端在收到此请求后,执行所有业务逻辑,然后使用套接字“戳”此大厅,以更新大厅中的所有客户端。
我的问题
除了用户关闭浏览器或选项卡的情况外,一切正常,因为在这种情况下我无法发送请求。 (据我所知,使用 javascript 和 beforeunload
事件、onDestroy()
方法等不可能做到这一点。)
我的问题
是否可以在服务器端检查是否有任何套接字断开连接,如果是,那么我该怎么做?我还尝试使用从前端发送到后端的 heartbeat
,但我不知道如何在服务器端处理此 heartbeat
消息。
服务器端(Spring boot)
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguartion implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/api/socket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
ThreadPoolTaskScheduler te = new ThreadPoolTaskScheduler();
te.setPoolSize(1);
te.setThreadNamePrefix("wss-heartbeat-thread-");
te.initialize();
config.enableSimpleBroker("/lobby")
.setHeartbeatValue(new long[]{0, 1000})
.setTaskScheduler(te);
}
}
@Controller
public class WebSocketController {
private final SimpMessagingTemplate template;
WebSocketController(SimpMessagingTemplate template) {
this.template = template;
}
public void pokeLobby(@DestinationVariable String lobbyName, SocketMessage message) {
this.template.convertAndSend("/lobby/"+lobbyName.toLowerCase(), message);
}
}
客户端
connectToLobbyWebSocket(lobbyName: string): void {
const ws = new SockJS(this.addressStorage.apiAddress + '/socket');
this.stompClient = Stomp.over(ws);
// this.stompClient.debug = null;
const that = this;
this.stompClient.connect({}, function () {
that.stompClient.subscribe('/lobby/' + lobbyName, (message) => {
if (message.body) {
that.socketMessage.next(message.body); // do client logic
}
});
});
}
最佳答案
您可以收听SessionDisconnectEvent在您的应用程序中,并在收到此类事件时向其他客户端发送消息。
Event raised when the session of a WebSocket client using a Simple Messaging Protocol (e.g. STOMP) as the WebSocket sub-protocol is closed. Note that this event may be raised more than once for a single session and therefore event consumers should be idempotent and ignore a duplicate event.
还有其他类型的events还有。
关于java - 如何处理socket断开和心跳消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55867629/
我正在使用血氧仪开展项目。我想对其进行平滑处理,以便可以使用它来计算心跳。我正在从麦克风收集原始数据,我将它们放在新数组中,比方说,sData[]。 信号真的很疯狂,如预期的那样在整个图上跳跃,所以我
如果您错过了 - TLS Heartbeat Extension 实现中的 OpenSSL 漏洞一直在流传。欲了解更多信息,请参阅http://heartbleed.com/ . 可能的缓解步骤之一是
随着关于 heartbleed 错误的讨论不断,很难找到关于 OpenSSL 的被利用的心跳扩展究竟用于什么的信息。 此外,是否可以针对 Apache w/mod_ssl 禁用它,而无需按照建议@ h
我是 Linux 新手,正在学习 LINUX heartbeat。我正在尝试做“yum install heartbeat”,如下所示…… # cat /etc/issueRed Hat Enterp
我在我们的服务器中使用带有kibana的heartbeat,metricbeat和elasticsearch 7.5,以检查服务器状态并检查应用程序的可用性。如果应用程序或服务器已关闭或没有响应,我想
我们有一个用 Java 编写的 RMI 客户端应用程序,它需要定期向服务器应用程序发送“保持 Activity ”消息。我们将其实现为一个单独的心跳线程,它向服务器发送保持 Activity 消息,然
我不确定如何最好地让我们的服务器知道我们的 iPhone 应用程序当前正在运行(即使在后台)。有几种不同的选择,但对于 Apple 批准程序允许的以及最可靠的选项存在一些担忧。如果考虑到 Apple
我似乎找不到关于如何在 Spring 中使用 websockets 向客户端发送心跳的好资源! 我有一个使用此配置运行的基本服务器: @Configuration @EnableWebSocketMe
我正在实现一个用于分布式 cronjob 执行的系统(所谓的 cron 计算集群)。当行动时间到来时,Cronjobs 应该排队进入消息队列(RabbitMQ)。在另一侧(集群的节点/ worker
使用 python kafka api 从主题中读取只有少量消息的消息。 Kafka 不断重复播放队列中的消息。 它从我的主题接收到一条消息(返回每条消息内容),然后抛出.更多日志: kafka.co
我目前有一个异步 TCP/IP 套接字 C# WinForms 应用程序,它是“服务器”应用程序。该应用程序持有客户端的持久套接字。我想实现一个心跳包来检测半开连接。 最初我只想创建一个循环并每隔 x
socket.io documentation提到可以像这样禁用心跳: io.disable('heartbeats'); 不幸的是,虽然这似乎阻止了心跳的发送,但客户端在不发送心跳时仍会断开连接。
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我是一名优秀的程序员,十分优秀!