I have wrote Websocket Client in html and javascript. and Websocket server in java.
var webSocket = new WebSocket('ws://localhost:8080/websocketendpoint');
@ServerEndpoint("/websocketendpoint")
public class WebSocketExample {
WebSocket connection to 'ws://localhost:8080/websocketendpoint' failed: Error during WebSocket handshake: Unexpected response code: 404
Can you please suggest how to create websocket uri and configure with the server
I had a small project with sockJS+stomp and Spring.
You need a config:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/user").withSockJS();
}
}
The idea is too create a controller on the server side:
@MessageMapping("/user")
@SendToUser("/topic/auth")
public JsonMessageWrapper process(JsonMessageWrapper jmw,
SimpMessageHeaderAccessor headerAccessor)
On the client side you subscribe this way:
function connect() {
var socket = new SockJS(/*[[@{/user}]]*/); // tricky mechanism of ThymeLeaf, produces a link attached to URL of deployed webapp with proper context
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
var subscribeUrl = '/topic/auth';
stompClient.subscribe( subscribeUrl + '-user' + socket.sessionId, function(response){
showMessage(response.body);
});
});
}
And this way you send a message:
function send() {
var message = document.getElementById('message').value;
stompClient.send("/app/user", {}, message);
}
PS I know @Transactional was used incorrectly.
Project's location https://github.com/dk2k/websocket-server
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.