forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
3,053 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
spring-mvc-java/src/main/java/com/baeldung/model/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.model; | ||
|
||
public class Message { | ||
|
||
private String from; | ||
private String text; | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public String getFrom() { | ||
return from; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
spring-mvc-java/src/main/java/com/baeldung/model/OutputMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.baeldung.model; | ||
|
||
public class OutputMessage { | ||
|
||
private String from; | ||
private String text; | ||
private String time; | ||
|
||
public OutputMessage(final String from, final String text, final String time) { | ||
|
||
this.from = from; | ||
this.text = text; | ||
this.time = time; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public String getTime() { | ||
return time; | ||
} | ||
|
||
public String getFrom() { | ||
return from; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.baeldung.spring.web.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(final MessageBrokerRegistry config) { | ||
config.enableSimpleBroker("/topic"); | ||
config.setApplicationDestinationPrefixes("/app"); | ||
} | ||
|
||
@Override | ||
public void registerStompEndpoints(final StompEndpointRegistry registry) { | ||
registry.addEndpoint("/chat").withSockJS(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
spring-mvc-java/src/main/java/com/baeldung/web/controller/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.baeldung.web.controller; | ||
|
||
import com.baeldung.model.Message; | ||
import com.baeldung.model.OutputMessage; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.handler.annotation.SendTo; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
@Controller | ||
public class ChatController { | ||
|
||
@MessageMapping("/chat") | ||
@SendTo("/topic/messages") | ||
public OutputMessage send(final Message message) throws Exception { | ||
|
||
final String time = new SimpleDateFormat("HH:mm").format(new Date()); | ||
return new OutputMessage(message.getFrom(), message.getText(), time); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<html> | ||
<head> | ||
<title>Chat WebSocket</title> | ||
|
||
<script src="./js/sockjs-0.3.4.js"></script> | ||
<script src="./js/stomp.js"></script> | ||
|
||
<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('/spring-mvc-java/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> | ||
|
||
</head> | ||
|
||
<body onload="disconnect()"> | ||
|
||
<div> | ||
|
||
|
||
<div> | ||
<input type="text" id="from" placeholder="Choose a nickname"/> | ||
</div> | ||
<br /> | ||
<div> | ||
<button id="connect" onclick="connect();">Connect</button> | ||
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button> | ||
</div> | ||
<br /> | ||
<div id="conversationDiv"> | ||
<input type="text" id="text" placeholder="Write a message..."/> | ||
<button id="sendMessage" onclick="sendMessage();">Send</button> | ||
<p id="response"></p> | ||
</div> | ||
</div> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.