2
respostas

Implementar WebSocket no Spring MVC

Tem algum tutorial, que eu possa me guiar para que consiga fazer uma implementação de WebSocket com Spring MVC sem Thymeleaf, pois a maioria dos tutoriais são feitos com Spring Boot e com Thymeleaf.

2 respostas

Oi Anderson, tudo que é falado em relação ao Spring Boot vai funcionar para um projeto com Spring Mvc tradicional :). As anotações são as mesmas e tudo mais. Caso tenha tentado, tenta colocar aqui o erro...

(index):36 Disconnected
stomp.js:130 Opening Web Socket...
sockjs-0.3.4.js:807 GET http://localhost:8080/web-sock-chat/chat/info 404 (Not Found)
AbstractXHRObject._start @ sockjs-0.3.4.js:807
(anonymous) @ sockjs-0.3.4.js:841
setTimeout (async)
utils.delay @ sockjs-0.3.4.js:352
utils.XHRLocalObject @ sockjs-0.3.4.js:840
InfoReceiver.doXhr @ sockjs-0.3.4.js:1936
(anonymous) @ sockjs-0.3.4.js:1928
setTimeout (async)
utils.delay @ sockjs-0.3.4.js:352
InfoReceiver @ sockjs-0.3.4.js:1928
createInfoReceiver @ sockjs-0.3.4.js:2006
SockJS @ sockjs-0.3.4.js:972
connect @ (index):20
onclick @ (index):63
stomp.js:130 Whoops! Lost connection to undefined

Recebo a seguinte mensagem no console ao executar a conexao com o SockeJS

<script type="text/javascript">
            var stompClient = null;

            function setConnected(connected) {
                document.getElementById('connect').disabled = connected;
                document.getElementById('disconnect').disabled = !connected;
                document.getElementById('conversationDiv').style.visibility 
                  = connected ? 'visible' : 'hidden';
                document.getElementById('response').innerHTML = '';
            }

            function connect() {
                var socket = new SockJS('/web-sock-chat/chat');
                stompClient = Stomp.over(socket);  
                stompClient.connect({}, function(frame) {
                    setConnected(true);
                    console.log('Connected: ' + frame);
                    stompClient.subscribe('/topic/messages', function(messageOutput) {
                        showMessageOutput(JSON.parse(messageOutput.body));
                    });
                });
            }

            function disconnect() {
                if(stompClient != null) {
                    stompClient.disconnect();
                }
                setConnected(false);
                console.log("Disconnected");
            }

            function sendMessage() {
                var from = document.getElementById('from').value;
                var text = document.getElementById('text').value;
                stompClient.send("/app/chat", {}, 
                  JSON.stringify({'from':from, 'text':text}));
            }

            function showMessageOutput(messageOutput) {
                var response = document.getElementById('response');
                var p = document.createElement('p');
                p.style.wordWrap = 'break-word';
                p.appendChild(document.createTextNode(messageOutput.from + ": " 
                  + messageOutput.text + " (" + messageOutput.time + ")"));
                response.appendChild(p);
            }
        </script>

Erro no Gist : https://gist.github.com/anonymous/09e526be8c9acb469f2302b2051db4ea