diff --git a/README.md b/README.md deleted file mode 100644 index bbf0e38..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# host-backend diff --git a/src/main/java/com/idetix/hostbackend/Controller/GreetingController.java b/src/main/java/com/idetix/hostbackend/Controller/GreetingController.java new file mode 100644 index 0000000..5d50617 --- /dev/null +++ b/src/main/java/com/idetix/hostbackend/Controller/GreetingController.java @@ -0,0 +1,27 @@ +package com.idetix.hostbackend.Controller; + +import com.idetix.hostbackend.Messages.Greeting; +import com.idetix.hostbackend.Messages.HelloMessage; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.messaging.simp.annotation.SendToUser; +import org.springframework.stereotype.Controller; + +@Controller +public class GreetingController { + @MessageMapping("/hello") + @SendToUser("/topic/greetings") + public Greeting greeting(HelloMessage message) throws Exception{ + Thread.sleep(1000); + return new Greeting("Hello, "+ message.getName() + "!"); + } + +// @MessageMapping("/hello") +// @SendTo("/topic/greetings") +// public Greeting greeting(HelloMessage message) throws Exception{ +// Thread.sleep(1000); +// return new Greeting("Hello, "+ message.getName() + "!"); +// } + + +} diff --git a/src/main/java/com/idetix/hostbackend/HostbackendApplication.java b/src/main/java/com/idetix/hostbackend/HostbackendApplication.java new file mode 100644 index 0000000..20950e5 --- /dev/null +++ b/src/main/java/com/idetix/hostbackend/HostbackendApplication.java @@ -0,0 +1,15 @@ +package com.idetix.hostbackend; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableScheduling +public class HostbackendApplication { + + public static void main(String[] args) { + SpringApplication.run(HostbackendApplication.class, args); + } + +} diff --git a/src/main/java/com/idetix/hostbackend/Messages/Greeting.java b/src/main/java/com/idetix/hostbackend/Messages/Greeting.java new file mode 100644 index 0000000..3a43141 --- /dev/null +++ b/src/main/java/com/idetix/hostbackend/Messages/Greeting.java @@ -0,0 +1,13 @@ +package com.idetix.hostbackend.Messages; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Greeting { + private String content; + +} diff --git a/src/main/java/com/idetix/hostbackend/Messages/HelloMessage.java b/src/main/java/com/idetix/hostbackend/Messages/HelloMessage.java new file mode 100644 index 0000000..573bc31 --- /dev/null +++ b/src/main/java/com/idetix/hostbackend/Messages/HelloMessage.java @@ -0,0 +1,13 @@ +package com.idetix.hostbackend.Messages; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class HelloMessage { + + private String name; +} diff --git a/src/main/java/com/idetix/hostbackend/WebSocketBrokerConfig.java b/src/main/java/com/idetix/hostbackend/WebSocketBrokerConfig.java new file mode 100644 index 0000000..588a9d2 --- /dev/null +++ b/src/main/java/com/idetix/hostbackend/WebSocketBrokerConfig.java @@ -0,0 +1,51 @@ +package com.idetix.hostbackend; + + + +import org.springframework.context.annotation.Configuration; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.http.server.ServletServerHttpRequest; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.server.support.DefaultHandshakeHandler; + +import javax.servlet.http.HttpSession; +import java.util.Map; + +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketBrokerConfig implements WebSocketMessageBrokerConfigurer { + + @Override + public void configureMessageBroker (MessageBrokerRegistry config) { + config.enableSimpleBroker("/topic"); + config.setApplicationDestinationPrefixes("/app"); + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry + .addEndpoint("/host-backend") + .setHandshakeHandler(new DefaultHandshakeHandler(){ + public boolean beforeHandshake( + ServerHttpRequest request, + ServerHttpResponse response, + WebSocketHandler wsHandler, + Map attributes) throws Exception{ + + if (request instanceof ServletServerHttpRequest){ + ServletServerHttpRequest servletRequest + = (ServletServerHttpRequest) request; + HttpSession session = servletRequest + .getServletRequest().getSession(); + attributes.put("sessionId", session.getId()); + } + return true; + } + }).withSockJS(); + } +} diff --git a/src/main/resources/static/app.js b/src/main/resources/static/app.js new file mode 100644 index 0000000..76a4f3e --- /dev/null +++ b/src/main/resources/static/app.js @@ -0,0 +1,50 @@ +var stompClient = null; + +function setConnected(connected) { + $("#connect").prop("disabled", connected); + $("#disconnect").prop("disabled", !connected); + if (connected) { + $("#conversation").show(); + } + else { + $("#conversation").hide(); + } + $("#greetings").html(""); +} + +function connect() { + var socket = new SockJS('/host-backend'); + stompClient = Stomp.over(socket); + stompClient.connect({}, function (frame) { + setConnected(true); + console.log('Connected: ' + frame); + stompClient.subscribe('/user/topic/greetings', function (greeting) { + showGreeting(JSON.parse(greeting.body).content); + }); + }); +} + +function disconnect() { + if (stompClient !== null) { + stompClient.disconnect(); + } + setConnected(false); + console.log("Disconnected"); +} + +function sendName() { + stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()})); +} + +function showGreeting(message) { + $("#greetings").append("" + message + ""); +} + +$(function () { + $("form").on('submit', function (e) { + e.preventDefault(); + }); + $( "#connect" ).click(function() { connect(); }); + $( "#disconnect" ).click(function() { disconnect(); }); + $( "#send" ).click(function() { sendName(); }); +}); \ No newline at end of file diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100644 index 0000000..b613267 --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,53 @@ + + + + Hello WebSocket + + + + + + + + + +
+
+
+
+
+ + + +
+
+
+
+
+
+ + +
+ +
+
+
+
+
+ + + + + + + + +
Greetings
+
+
+
+ + \ No newline at end of file diff --git a/src/test/java/com/idetix/hostbackend/HostbackendApplicationTests.java b/src/test/java/com/idetix/hostbackend/HostbackendApplicationTests.java new file mode 100644 index 0000000..777ab5c --- /dev/null +++ b/src/test/java/com/idetix/hostbackend/HostbackendApplicationTests.java @@ -0,0 +1,5 @@ +package com.idetix.hostbackend; + +class HostbackendApplicationTests { + +}