Rohan Sircar
4 years ago
5 changed files with 156 additions and 14 deletions
-
20pom.xml
-
12src/main/java/org/ros/chatto/websocket/Message.java
-
24src/main/java/org/ros/chatto/websocket/WebSocketConfig.java
-
29src/main/java/org/ros/chatto/websocket/WebSocketController.java
-
85src/main/resources/templates/ws.html
@ -0,0 +1,12 @@ |
|||
package org.ros.chatto.websocket; |
|||
|
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
@Builder |
|||
public class Message { |
|||
private String from; |
|||
private String to; |
|||
private String text; |
|||
} |
@ -0,0 +1,24 @@ |
|||
package org.ros.chatto.websocket; |
|||
|
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; |
|||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; |
|||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; |
|||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; |
|||
|
|||
@Configuration |
|||
@EnableWebSocketMessageBroker |
|||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { |
|||
|
|||
@Override |
|||
public void configureMessageBroker(MessageBrokerRegistry config) { |
|||
config.enableSimpleBroker("/topic", "/queue"); |
|||
config.setApplicationDestinationPrefixes("/app"); |
|||
} |
|||
|
|||
@Override |
|||
public void registerStompEndpoints(StompEndpointRegistry registry) { |
|||
registry.addEndpoint("/chat"); |
|||
registry.addEndpoint("/chat").withSockJS(); |
|||
} |
|||
} |
@ -0,0 +1,29 @@ |
|||
package org.ros.chatto.websocket; |
|||
|
|||
import org.springframework.messaging.handler.annotation.MessageMapping; |
|||
import org.springframework.messaging.handler.annotation.Payload; |
|||
import org.springframework.messaging.simp.SimpMessagingTemplate; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
|
|||
@Controller |
|||
@RequiredArgsConstructor |
|||
public class WebSocketController { |
|||
|
|||
private final SimpMessagingTemplate smt; |
|||
|
|||
@MessageMapping("/chat") |
|||
// @SendTo("/topic/messages") |
|||
// @SendToUser("/queue/reply") |
|||
public void send(@Payload final Message message) throws Exception { |
|||
smt.convertAndSendToUser(message.getTo(), "/queue/reply", message); |
|||
// return message; |
|||
} |
|||
|
|||
@GetMapping("/ws") |
|||
public String wsPage() { |
|||
return "ws"; |
|||
} |
|||
} |
@ -0,0 +1,85 @@ |
|||
<html> |
|||
|
|||
<head> |
|||
<title>Chat WebSocket</title> |
|||
<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.5.0/sockjs.min.js"></script> |
|||
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.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('/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)); |
|||
// }); |
|||
stompClient.subscribe('/user/queue/reply', 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 to = document.getElementById('to').value; |
|||
var text = document.getElementById('text').value; |
|||
stompClient.send("/app/chat", {}, |
|||
JSON.stringify({ 'from': from, 'text': text, to: to })); |
|||
} |
|||
|
|||
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> |
|||
<div> |
|||
<input type="text" id="to" placeholder="Send to User" /> |
|||
</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> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue