-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes needed to broadcast only to room #5
Comments
Hi @kinjalnyc - sorry for the late reply. I didn't spend much time, but here's one way you can create multiple rooms, and have chat messages delivered to the targeted ones.
@Bean
public HandlerMapping webSocketHandlerMapping(ChatWebSocketHandler webSocketHandler) {
Map<String, WebSocketHandler> map = new HashMap<>();
map.put("/redis-chat/**", webSocketHandler);
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setCorsConfigurations(Collections.singletonMap("*", new CorsConfiguration().applyPermitDefaultValues()));
handlerMapping.setOrder(1);
handlerMapping.setUrlMap(map);
return handlerMapping;
}
public class ChatMessage {
private Integer id;
private String message;
private String hostname;
private Long usersOnline;
private String room;
}
public Mono<Long> publishChatMessage(String message, String room) {
Integer totalChatMessage = chatMessageCounter.incrementAndGet();
return Mono.fromCallable(() -> {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
log.error("Error getting hostname.", e);
}
return "localhost";
}).map(hostName -> {
ChatMessage chatMessage = new ChatMessage(totalChatMessage, message, hostName, activeUserCounter.get(), room);
String chatString = "EMPTY_MESSAGE";
try {
chatString = objectMapper.writeValueAsString(chatMessage);
} catch (JsonProcessingException e) {
log.error("Error converting ChatMessage {} into string", chatMessage, e);
}
return chatString;
}).flatMap(chatString -> {
// Publish Message to Redis Channels
return reactiveStringRedisTemplate.convertAndSend(MESSAGE_TOPIC, chatString)
.doOnSuccess(aLong -> log.debug("Total of {} Messages published to Redis Topic.", totalChatMessage))
.doOnError(throwable -> log.error("Error publishing message.", throwable));
});
}
let host = location.hostname + (location.port ? ':' + location.port : '');
let wsProtocol = location.protocol === "https:" ? "wss://" : "ws://";
// connecting room=one
let room1 = new WebSocket(wsProtocol + host + "/redis-chat?room=one");
room1.onopen = openEvent => {
console.log("room1 opened", openEvent);
};
room1.onmessage = messageEvent => {
console.log("Message: from room1 ", messageEvent);
};
// connecting room=two
let room2 = new WebSocket(wsProtocol + host + "/redis-chat?room=two");
room2.onopen = openEvent => {
console.log("room2 opened", openEvent);
};
room2.onmessage = messageEvent => {
console.log("Message: from room2 ", messageEvent);
};
// Send messages:
room1.send('This message goes to all connected to room-1');
room2.send('This message goes to all connected to room-2');
room1.send('This message goes to all connected to room-1'); Hope this help! |
This is really awesome, thank you.
Quick question, if I want to modify this to support multiple rooms how should I proceed ?
The text was updated successfully, but these errors were encountered: