Sto provando a implementare una notifica push in primavera utilizzando websocket e con l'uso di sock.js.Websocket: Come inviare un messaggio a un utente target
Questi sono i frammenti di codice:
public class NotifyController {
@MessageMapping("/notifications")
@SendTo("/get/notifications")
public Greeting greeting(HelloMessage message) throws Exception {
new Greeting("Hello, " + message.getName() + "!");
}
}
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/get/notifications");
config.setApplicationDestinationPrefixes("/gssocket");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/notifications").withSockJS();
}
}
Questo è il codice nella parte anteriore ..
function connect() {
var notificationSocket = new SockJS('/notifications');
stompNotificationClient = Stomp.over(notificationSocket);
stompNotificationClient.connect({}, function(frame) {
console.log('Connected: ' + frame);
stompNotificationClient.subscribe('/get/notifications', function(greeting){
showGreeting(JSON.parse(greeting.body).content);
});
});
}
function sendNotification() {
var name = "test"
stompNotificationClient.send("/gssocket/notifications", {}, JSON.stringify({ 'name': name }));
}
ho già riuscito a farlo funzionare. Ma la domanda è: come posso inviare il messaggio a determinati utenti target. Ad esempio, ci sono 5 utenti online: utente1, utente2, utente3, utente4 e utente5. Voglio che la notifica sia pubblicata solo su user1 e user2. Puoi darmi qualche idea su come ottenere questo? Sto pensando di farlo nel back-end o nel frontend. O c'è un altro modo per ottenere questo usando la primavera.
Qualcuno mi aiuti per favore.
Grazie.
http://stackoverflow.com/questions/23594671/spring-websocket-notification/23596605#23596605 –