pom.xml
4.0.0 org.test.blog sample 0.1.0 org.springframework.boot spring-boot-starter-parent 1.3.0.RELEASE 1.7 org.springframework.boot spring-boot-starter-web org.projectlombok lombok 1.16.6 org.springframework.boot spring-boot-starter-websocket org.springframework spring-messaging org.springframework.boot spring-boot-maven-plugin
src/main/java/TestApplication
package org.test.blog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class);
}
}
src/main/java/GreetingController
package org.test.blog.controller;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.test.blog.model.Greeting;
import org.test.blog.model.HelloMessage;
@Controller
public class GreetingController {
@MessageMapping(value = "/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws InterruptedException {
return new Greeting("Hello, " + message.getName() + "!");
}
}
src/main/java/Greeting
package org.test.blog.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Greeting {
private String content;
}
src/main/java/HelloMessage
package org.test.blog.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HelloMessage {
private String name;
}
src/main/java/WebSocketConfig
package org.test.blog.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}
classpath:/static/test.html
Hello WebSocket
You can find the action that a server send the message by using "/topic/greetings" url
to client, after a clent send the request by using "/app/hello" url.
original source : https://spring.io/guides/gs/messaging-stomp-websocket/
댓글 없음 :
댓글 쓰기