Örnek
Mesajı dinleyen STOMP dinleyicisi şöyledir. Burada SpringBoot uygulamasındaki servlet şöyle. server.servlet.context-path=/consumer
Burada Spring Servlet "/consumer" adresini dinliyor. STOMP broker'da "/consume" adresini dinliyor. Dolayısıyla istemci "/consumer/consume" adresine bağlanıyor.
var stompClient = null;function connect(){var socket = new SockJS('/consumer/consume');stompClient = Stomp.over(socket);stompClient.connect({}, onConnected, onError);}function onError(event){alert(event);}function onConnected() {stompClient.subscribe('/topic.socket.rabbit', onMessageReceived);}function onMessageReceived(payload) {var message = JSON.parse(payload.body);var tableBody = document.getElementById("tableBody");var newRow = tableBody.insertRow();var cell1 = newRow.insertCell(0);cell1.appendChild(document.createTextNode(message.message));var cell2 = newRow.insertCell(1);cell2.appendChild(document.createTextNode(message.time));}window.onload = connect;
Örnek
Şöyle yaparız
var stompClient = null;function connect(event) {var socket = new SockJS('/websocket');stompClient = Stomp.over(socket);stompClient.connect({}, onConnected, onError);event.preventDefault();}function onConnected() {// Subscribe to the Public TopicstompClient.subscribe('/topic/public', onMessageReceived);// Tell your username to the serverstompClient.send("/app/chat.register",{},JSON.stringify({sender: username, type: 'JOIN'}))}function onError(error) {...}function send(event) {var messageContent = messageInput.value.trim();if(messageContent && stompClient) {var chatMessage = {sender: username,content: messageInput.value,type: 'CHAT'};stompClient.send("/app/chat.send", {}, JSON.stringify(chatMessage));}event.preventDefault();}function onMessageReceived(payload) {var message = JSON.parse(payload.body);if(message.type === 'JOIN') {...} else if (message.type === 'LEAVE') {...} else {...}usernameForm.addEventListener('submit', connect, true)
Hiç yorum yok:
Yorum Gönder