Browse Source

websocket support v2

master
gsd 1 year ago
parent
commit
146c3d8145
  1. 5
      src/main/java/app/controllers/debug/WebSocketPage.java
  2. 113
      src/main/java/app/websocket/IntegrationWebSockerHandler.java
  3. 8
      src/main/java/app/websocket/WebSocketConfig.java
  4. 2
      src/main/java/app/websocket/WebSocketHandler.java

5
src/main/java/app/controllers/debug/WebSocketPage.java

@ -2,6 +2,7 @@ package app.controllers.debug;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@ -9,7 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
public class WebSocketPage {
@GetMapping
public String getPage(){
public String getPage(@RequestParam(required = false, defaultValue = "ws") String route){
return "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
@ -56,7 +57,7 @@ public class WebSocketPage {
"\n" +
"\n" +
"<script>\n" +
" const socketConn = new WebSocket('ws://localhost:8080/ws');\n" +
" const socketConn = new WebSocket('ws://localhost:8080/" + route + "');\n" +
"\n" +
" function send() {\n" +
" const clientMsg = document.getElementById('myMessage');\n" +

113
src/main/java/app/websocket/IntegrationWebSockerHandler.java

@ -0,0 +1,113 @@
package app.websocket;
import app.entities.VipGiveMethod;
import app.services.db.VIPService;
import app.utils.SaltedCookie;
import app.utils.SteamIDConverter;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException;
import java.time.Instant;
import java.util.HashSet;
@Component
@Scope(value = "singleton")
public class IntegrationWebSockerHandler extends TextWebSocketHandler {
private final Logger logger = LoggerFactory.getLogger(getClass());
private ObjectMapper objectMapper = new ObjectMapper();
private SaltedCookie saltedCookie;
private VIPService vipService;
private HashSet<String> unique_set;
@Autowired
public IntegrationWebSockerHandler(SaltedCookie saltedCookie, VIPService vipService) {
super();
this.saltedCookie = saltedCookie;
this.vipService = vipService;
this.unique_set = new HashSet();
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
logger.info("Session {} open", session.getId());
super.afterConnectionEstablished(session);
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
IntegrationPayload payload = objectMapper.readValue(message.getPayload(), IntegrationPayload.class);
if (payload.test) {
session.sendMessage(new TextMessage("%d".formatted(Instant.now().getEpochSecond())));
return;
}
if (payload.isVip()) {
if (!saltedCookie.ValidateSecretKey(payload.getSecret_key())) {
logger.warn("Cannot add vip without secret key");
return;
}
if (!payload.vip.isFilled()) {
logger.warn("Can try add VIP but, payload is not full filled!");
return;
}
if (!payload.vip.unique.isEmpty()) {
if (unique_set.contains(payload.vip.unique)) {
logger.warn("VIP uniq payload value already exist! " + payload.vip.unique);
return;
} else unique_set.add(payload.vip.unique);
}
Integer result = vipService.addVIP(
SteamIDConverter.getSteamID(payload.vip.getSteam()),
payload.vip.getAmount(),
VipGiveMethod.valueOf(payload.vip.service.toUpperCase()),
payload.vip.getExtra());
logger.info("Success add VIP to {}, result: {}", payload.vip.getSteam(), result);
return;
}
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
logger.info("Session {} close", session.getId());
super.afterConnectionClosed(session, status);
}
@Data
@NoArgsConstructor
public static class IntegrationPayload {
String secret_key = null;
ExternalVIP vip = null;
Boolean test = false;
public boolean isVip() {
return vip != null && secret_key != null;
}
}
@Data
@NoArgsConstructor
public static class ExternalVIP {
String steam;
Integer amount;
String service;
String extra = "";
String unique = "";
public boolean isFilled() {
return steam != null && amount != null && service != null;
}
}
}

8
src/main/java/app/websocket/WebSocketConfig.java

@ -10,13 +10,19 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry
public class WebSocketConfig implements WebSocketConfigurer {
private WebSocketHandler webSocketHandler;
private IntegrationWebSockerHandler integrationWebSockerHandler;
public WebSocketConfig(WebSocketHandler webSocketHandler) {
public WebSocketConfig(
WebSocketHandler webSocketHandler,
IntegrationWebSockerHandler integrationWebSockerHandler
) {
this.webSocketHandler = webSocketHandler;
this.integrationWebSockerHandler = integrationWebSockerHandler;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler, "/ws");
registry.addHandler(integrationWebSockerHandler, "/ws_integrations");
}
}

2
src/main/java/app/websocket/WebSocketHandler.java

@ -20,7 +20,7 @@ import java.util.Map;
@Component
@Scope(value = "singleton")
public class WebSocketHandler extends TextWebSocketHandler {
private final Logger logger = LoggerFactory.getLogger(BaseUpdater.class);
private final Logger logger = LoggerFactory.getLogger(getClass());
private final Map<String, WebSocketSession> activeSessions = new HashMap<>();
private ObjectMapper objectMapper = new ObjectMapper();

Loading…
Cancel
Save