好吧,我几乎读过春季websocket上的每一篇文章。我想发送一条消息到一个特定的客户端,一旦它连接到服务器,简单!
我已经能够从客户端连接,并且可以监听服务器端的新连接。但是,当我试图从服务器端发送消息时,什么都不会发生。尽管我的客户已经订阅了。
下面是我的当事人js。
var stompClient = null;
$(document).ready(function() {
connect();
});
function connect() {
var socket = new SockJS('/context_path/stockfeeds');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/share', function(message) {
console.log("Subscription successful............");
console.log(JSON.parse(message.body));
});
stompClient.subscribe('/user/queue/reply', function(message) {
console.log("Subscribed to user ...");
});
}, function (error) {
console.log("Stomp protocol error: "+ error);
});
}
$('#livetext').click(function () {
stompClient.send('/app/stockfeeds', {}, 'This is BS');
});当我单击'#livetext‘时,发送函数可以工作。并且在客户端上接收响应。下面是我的主计长。
@Controller
public class StockDispatchController {
@Autowired
LoggerService loggerService;
@MessageMapping("/stockfeeds")
@SendTo("/topic/share")
public String fetchStocks(String message) throws Exception {
loggerService.info("-----------------------------"+ message);
return message;
}
}下面是我的连接事件侦听器
import org.springframework.messaging.simp.SimpMessagingTemplate;
@Component
public class StompConnectEvent implements ApplicationListener<SessionConnectEvent> {
@Autowired
LoggerService logger;
@Autowired
private SimpMessagingTemplate messagingTemplate;
public void onApplicationEvent(SessionConnectEvent event) {
this.messagingTemplate.convertAndSend("/topic/share", "A new client just connected");
this.messagingTemplate.convertAndSendToUser(event.getUser().getName(), "/queue/reply", "I just connected");
logger.info("message template sent");
}
}当一个新客户端连接时,事件侦听器就会被触发并运行到最后,但是我没有在客户机上得到任何响应。
最后,下面是我的应用程序上下文xml
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/stockfeeds">
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic, /queue"/>
</websocket:message-broker>发布于 2018-04-23 17:10:45
我还没有找到解决方案,但我找到了一个可行的替代方案。因此,我没有监听SessionConnectedEvent,而是添加了一个控制器方法,当用户订阅它时会触发它。
见下文。
@SubscribeMapping("/reply")
public String initialReply() throws Exception {
return "Welcome to the chat room.";
}我按以下方式订阅它。
stompClient.subscribe('/app/reply', function(message) {
alert(message.body);
});发布于 2019-07-03 13:16:17
我有类似的用例,这就是我成功实现的地方。
clientId23,您可以通过已建立的websocket将其发送到服务器。/app/reply/clientId23 )中添加相同的随机字符串。this.messagingTemplate.convertAndSend("/app/reply/clientId23", "A new client just connected");编码愉快!
发布于 2021-07-11 13:13:51
很晚了,不过万一有人需要这个。我没有使用对我不起作用的SessionConnectEvent,而是在阅读了文档之后使用了SessionSubscribeEvent,它起了作用。
@Component
public class StompConnectEvent implements ApplicationListener<SessionSubscribeEvent> {
@Autowired
private SimpMessagingTemplate messagingTemplate;
public void onApplicationEvent(SessionSubscribeEvent event) {
this.messagingTemplate.convertAndSend("/topic/share", messageObject);
}
}一旦客户端订阅了websocket端点,SessionSubscribeEvent就会被调用,因此它可以接收服务器发送到端点的消息。但是当客户端最初尝试连接到websocket时会调用SessionConnectEvent,因此客户机还没有侦听端点。
https://stackoverflow.com/questions/49980319
复制相似问题