first successful ws impl
This commit is contained in:
parent
74700bfdc2
commit
4329c5067f
14 changed files with 175 additions and 154 deletions
|
@ -1,13 +1,16 @@
|
|||
package me.imsonmia.epqapi;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EpqapiApplication {
|
||||
|
||||
public class EpqapiApplication extends Thread {
|
||||
private static Logger logger = LoggerFactory.getLogger(EpqapiApplication.class);
|
||||
public static void main(String[] args) {
|
||||
logger.info("Main Spring Boot process running in thread!");
|
||||
SpringApplication.run(EpqapiApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
21
src/main/java/me/imsonmia/epqapi/config/WebSocketConfig.java
Normal file
21
src/main/java/me/imsonmia/epqapi/config/WebSocketConfig.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package me.imsonmia.epqapi.config;
|
||||
|
||||
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 registry) {
|
||||
registry.enableSimpleBroker("/sub");
|
||||
registry.setApplicationDestinationPrefixes("/app");
|
||||
}
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/ws").setAllowedOrigins("*");
|
||||
}
|
||||
}
|
|
@ -1,19 +1,21 @@
|
|||
package me.imsonmia.epqapi.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import me.imsonmia.epqapi.model.ChatMessage;
|
||||
import me.imsonmia.epqapi.repository.ChatMessageRepository;
|
||||
import me.imsonmia.epqapi.model.Message;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1")
|
||||
@Controller
|
||||
// @RequestMapping("/api/v1")
|
||||
public class ChatMessageController {
|
||||
private ChatMessageRepository chatMessageRepository;
|
||||
@GetMapping("/msg/{id}")
|
||||
public ChatMessage getMessageById(@PathVariable(value = "id") Long id) {
|
||||
return chatMessageRepository.findById(id).get();
|
||||
@MessageMapping("/chat")
|
||||
@SendTo("/sub/chat")
|
||||
public Message messageHandler(Message message) throws Exception {
|
||||
return message;
|
||||
}
|
||||
// @GetMapping("/msg/{id}")
|
||||
// public ChatMessage getMessageById(@PathVariable(value = "id") Long id) {
|
||||
// return chatMessageRepository.findById(id).get();
|
||||
// }
|
||||
}
|
||||
|
|
17
src/main/java/me/imsonmia/epqapi/controller/TextMessage.java
Normal file
17
src/main/java/me/imsonmia/epqapi/controller/TextMessage.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package me.imsonmia.epqapi.controller;
|
||||
|
||||
public class TextMessage {
|
||||
private String text;
|
||||
public TextMessage() {
|
||||
|
||||
}
|
||||
public TextMessage(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package me.imsonmia.epqapi.messaging;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import jakarta.websocket.DecodeException;
|
||||
import jakarta.websocket.Decoder;
|
||||
import jakarta.websocket.EndpointConfig;
|
||||
|
||||
public class MessageDecoder implements Decoder.Text<Message> {
|
||||
private static Gson gson = new Gson();
|
||||
@Override
|
||||
public Message decode(String message) throws DecodeException {
|
||||
return gson.fromJson(message, Message.class);
|
||||
}
|
||||
@Override
|
||||
public void destroy() {
|
||||
Text.super.destroy();
|
||||
}
|
||||
@Override
|
||||
public void init(EndpointConfig endpointConfig) {
|
||||
Text.super.init(endpointConfig);
|
||||
}
|
||||
@Override
|
||||
public boolean willDecode(String s) {
|
||||
return (s != null);
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package me.imsonmia.epqapi.messaging;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import jakarta.websocket.EncodeException;
|
||||
import jakarta.websocket.Encoder;
|
||||
import jakarta.websocket.EndpointConfig;
|
||||
|
||||
public class MessageEncoder implements Encoder.Text<Message> {
|
||||
private static Gson gson = new Gson();
|
||||
@Override
|
||||
public String encode(Message message) throws EncodeException {
|
||||
return gson.toJson(message);
|
||||
}
|
||||
@Override
|
||||
public void destroy() {
|
||||
Text.super.destroy();
|
||||
}
|
||||
@Override
|
||||
public void init(EndpointConfig endpointConfig) {
|
||||
Text.super.init(endpointConfig);
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
package me.imsonmia.epqapi.messaging;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import jakarta.websocket.EncodeException;
|
||||
import jakarta.websocket.OnClose;
|
||||
import jakarta.websocket.OnMessage;
|
||||
import jakarta.websocket.OnOpen;
|
||||
import jakarta.websocket.Session;
|
||||
import jakarta.websocket.server.ServerEndpoint;
|
||||
import me.imsonmia.epqapi.model.ChatConvert;
|
||||
import me.imsonmia.epqapi.model.ChatMessage;
|
||||
import me.imsonmia.epqapi.repository.ChatMessageRepository;
|
||||
|
||||
// https://www.baeldung.com/java-websockets
|
||||
@ServerEndpoint(
|
||||
value = "/chat/{username}",
|
||||
encoders = MessageEncoder.class,
|
||||
decoders = MessageDecoder.class
|
||||
)
|
||||
public class WebSocketHandler {
|
||||
private Logger logger;
|
||||
private ChatMessageRepository chatMessageRepository;
|
||||
private Session session;
|
||||
private Set<WebSocketHandler> chatEndpoints = new CopyOnWriteArraySet<>();
|
||||
private HashMap<String, String> users = new HashMap<>();
|
||||
@OnOpen
|
||||
public void onOpen(Session session, String username) throws IOException {
|
||||
this.session = session;
|
||||
chatEndpoints.add(this);
|
||||
users.put(session.getId(), username);
|
||||
Message message = new Message();
|
||||
message.setFrom(username);
|
||||
message.setContent("Connected, hello world!");
|
||||
broadcast(message);
|
||||
ChatMessage cmessage = new ChatConvert().fromMessage(message);
|
||||
chatMessageRepository.save(cmessage);
|
||||
}
|
||||
@OnMessage
|
||||
public void onMessage(Session session, Message message) throws IOException {
|
||||
message.setFrom(users.get(session.getId()));
|
||||
broadcast(message);
|
||||
}
|
||||
@OnClose
|
||||
public void onClose(Session session) throws IOException {
|
||||
chatEndpoints.remove(this);
|
||||
Message message = new Message();
|
||||
message.setFrom(users.get(session.getId()));
|
||||
message.setContent("Disconnected!");
|
||||
broadcast(message);
|
||||
}
|
||||
public void broadcast(Message message) {
|
||||
chatEndpoints.forEach(endpoint -> {
|
||||
try {
|
||||
endpoint.session.getBasicRemote().sendObject(message);
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.info("Error sending message! IOException");
|
||||
logger.error(null, e);
|
||||
}
|
||||
catch (EncodeException e) {
|
||||
logger.info("Error sending message! EncodeException");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
package me.imsonmia.epqapi.model;
|
||||
|
||||
import me.imsonmia.epqapi.messaging.Message;
|
||||
import me.imsonmia.epqapi.repository.UserRepository;
|
||||
|
||||
public class ChatConvert {
|
||||
private UserRepository r;
|
||||
public ChatMessage fromMessage(Message s) {
|
||||
return new ChatMessage(r.findByFirstName(s.getFrom()).getId(), s.getContent());
|
||||
return new ChatMessage(r.findByUserName(s.getFrom()).getId(), s.getContent());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package me.imsonmia.epqapi.messaging;
|
||||
package me.imsonmia.epqapi.model;
|
||||
|
||||
public class Message {
|
||||
private String from;
|
|
@ -5,5 +5,5 @@ import org.springframework.data.repository.CrudRepository;
|
|||
import me.imsonmia.epqapi.model.User;
|
||||
|
||||
public interface UserRepository extends CrudRepository<User, Long> {
|
||||
User findByFirstName(String userName);
|
||||
User findByUserName(String userName);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue