删除无用代码 WebSocket

This commit is contained in:
liuchengqian 2023-02-14 13:50:12 +08:00
parent e14962bc5f
commit e44f1a301f
8 changed files with 0 additions and 271 deletions

View File

@ -1,25 +0,0 @@
package com.xkrs.websocket.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;
/**
* websocket广播配置
* @author xkrs
*/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketBrokerConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/endPointClientServer").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/ws");
}
}

View File

@ -1,16 +0,0 @@
package com.xkrs.websocket.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @author xkrs
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}

View File

@ -1,18 +0,0 @@
package com.xkrs.websocket.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author xkrs
*/
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/hello")
public String sayHello(){
return "asdasd";
}
}

View File

@ -1,24 +0,0 @@
package com.xkrs.websocket.controller.websocket;
import com.xkrs.websocket.service.WebSocketServer;
import org.springframework.web.bind.annotation.*;
/**
* @author xkrs
*/
@RestController
@RequestMapping("/api/socket")
public class WebSocketController {
@GetMapping("/sendAll")
public String sendAll(@RequestParam String msg) {
WebSocketServer.broadInfo(msg);
return "success";
}
@RequestMapping(value = "/sendOne", method = RequestMethod.GET)
public String sendOne(@RequestParam String msg, @RequestParam String session) {
WebSocketServer.sendMsg(msg, session);
return "success";
}
}

View File

@ -1,21 +0,0 @@
package com.xkrs.websocket.controller.websocketbroker;
import com.xkrs.websocket.websocket.ClientServerMessage;
import com.xkrs.websocket.websocket.ServerClientMessage;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
/**
* @author xkrs
*/
@Controller
public class WebSocketBrokerController {
@MessageMapping("/hello")
@SendTo("/ws/getResponse")
public ServerClientMessage say(ClientServerMessage message) throws InterruptedException {
Thread.sleep(3000);
return new ServerClientMessage("hello," + message.getName() + "!");
}
}

View File

@ -1,128 +0,0 @@
package com.xkrs.websocket.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author xkrs
*/
@Component
@ServerEndpoint(value = "/ws/asset")
public class WebSocketServer {
@PostConstruct
public void init() {
System.out.println("websocketBroker loading......");
}
private static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
private static final AtomicInteger ONLINE_COUNT = new AtomicInteger(0);
private static CopyOnWriteArraySet<Session> sessionSet = new CopyOnWriteArraySet<>();
/**
* 连接建立
*/
@OnOpen
public void onOpen(Session session) {
sessionSet.add(session);
//在线加1
int cnt = ONLINE_COUNT.incrementAndGet();
//log.info("have a new connect:{}", cnt);
//sendMsg(session, "connect success");
}
/**
* 连接关闭
*
* @param session
*/
@OnClose
public void onClose(Session session) {
sessionSet.remove(session);
//在线减1
int cnt = ONLINE_COUNT.decrementAndGet();
log.info("have a connect close:{}", cnt);
}
/**
* 收到客户端消息后调用的方法
*
* @param session
* @param msg
*/
@OnMessage
public void onMessage(String msg, Session session) {
log.info("receive the msg:{}", msg);
}
private static void sendMsg(Session session, String msg) {
try {
session.getBasicRemote().sendText(String.format(msg));
} catch (IOException e) {
log.error("发送信息出错:{}", e.getMessage());
e.printStackTrace();
}
}
private static void sendDate(Session session, Map map) {
try {
session.getBasicRemote().sendObject(map);
} catch (IOException | EncodeException e) {
log.error("发送信息出错:{}", e.getMessage());
e.printStackTrace();
}
}
/**
* 指定session发送
*/
public static void sendMsg(String msg, String sessionId) {
Session session = null;
for (Session s : sessionSet) {
if (s.getId().equals(sessionId)) {
session = s;
break;
}
}
if (session != null) {
sendMsg(session, msg);
} else {
log.warn("not find your session");
}
}
/**
* 群发消息
*/
public static void broadInfo(String msg) {
for (Session s : sessionSet) {
synchronized (sessionSet){
if (s.isOpen()) {
sendMsg(s, msg);
}
}
}
}
/**
* 群发map消息
* @param map
*/
public static void broadInfoMap(Map map) {
for (Session s : sessionSet) {
if (s.isOpen()) {
sendDate(s, map);
}
}
}
}

View File

@ -1,18 +0,0 @@
package com.xkrs.websocket.websocket;
/**
* @author xkrs
*/
public class ClientServerMessage {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}

View File

@ -1,21 +0,0 @@
package com.xkrs.websocket.websocket;
/**
* @author xkrs
*/
public class ServerClientMessage {
public String getResponseMsg() {
return responseMsg;
}
public void setResponseMsg(String responseMsg) {
this.responseMsg = responseMsg;
}
private String responseMsg;
public ServerClientMessage(String responseMsg) {
this.responseMsg = responseMsg;
}
}