WebSocket
This commit is contained in:
parent
5f2973e4a2
commit
9ad0bc0ed5
@ -11,6 +11,8 @@ import java.net.*;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用http发送方法
|
* 通用http发送方法
|
||||||
@ -319,4 +321,27 @@ public class HttpUtils {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取请求路径参数
|
||||||
|
* @param url 请求路径
|
||||||
|
*/
|
||||||
|
public static Map<String,Object> getQueryParams(String url) {
|
||||||
|
try {
|
||||||
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
|
for (String param : url.split("&")) {
|
||||||
|
String[] pair = param.split("=");
|
||||||
|
String key = URLDecoder.decode(pair[0], "UTF-8");
|
||||||
|
String value = "";
|
||||||
|
if (pair.length > 1) {
|
||||||
|
value = URLDecoder.decode(pair[1], "UTF-8");
|
||||||
|
}
|
||||||
|
params.put(key, value);
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
} catch (UnsupportedEncodingException ex) {
|
||||||
|
throw new AssertionError(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,7 +42,6 @@ public class MiniProgramAuthenticationFilter extends AbstractAuthenticationProce
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
String principal = (String) authenticationBean.get("phone");
|
String principal = (String) authenticationBean.get("phone");
|
||||||
|
|
||||||
String openId = (String) authenticationBean.get("openId");
|
String openId = (String) authenticationBean.get("openId");
|
||||||
String appId = (String) authenticationBean.get("appId");
|
String appId = (String) authenticationBean.get("appId");
|
||||||
String brandId = (String) authenticationBean.get("brandId");
|
String brandId = (String) authenticationBean.get("brandId");
|
||||||
|
|||||||
@ -19,6 +19,11 @@
|
|||||||
<groupId>com.cpop</groupId>
|
<groupId>com.cpop</groupId>
|
||||||
<artifactId>Cpop-Core</artifactId>
|
<artifactId>Cpop-Core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--webSocket-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -0,0 +1,22 @@
|
|||||||
|
package com.cpop.system.framework.ws.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||||
|
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DB
|
||||||
|
* @createTime 2023/11/23 18:22
|
||||||
|
* @description webSocket配置
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
//开启webSocket
|
||||||
|
@EnableWebSocket
|
||||||
|
public class WebSocketConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServerEndpointExporter serverEndpointExporter() {
|
||||||
|
return new ServerEndpointExporter();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,146 @@
|
|||||||
|
package com.cpop.system.framework.ws.server;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.websocket.*;
|
||||||
|
import javax.websocket.server.PathParam;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DB
|
||||||
|
* @createTime 2023/11/23 18:24
|
||||||
|
* @description 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,
|
||||||
|
* 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
@ServerEndpoint("/websocket/{sid}")
|
||||||
|
public class WebSocketServer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
|
||||||
|
*/
|
||||||
|
private static int onlineCount = 0;
|
||||||
|
/**
|
||||||
|
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
||||||
|
*/
|
||||||
|
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
||||||
|
*/
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收sid
|
||||||
|
*/
|
||||||
|
private String sid = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接建立成功调用的方法
|
||||||
|
*/
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session, @PathParam("sid") String sid) {
|
||||||
|
this.session = session;
|
||||||
|
//加入set中
|
||||||
|
webSocketSet.add(this);
|
||||||
|
this.sid = sid;
|
||||||
|
//在线数加1
|
||||||
|
addOnlineCount();
|
||||||
|
try {
|
||||||
|
sendMessage("conn_success");
|
||||||
|
log.info("有新窗口开始监听:" + sid + ",当前在线人数为:" + getOnlineCount());
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("websocket IO Exception");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接关闭调用的方法
|
||||||
|
*/
|
||||||
|
@OnClose
|
||||||
|
public void onClose() {
|
||||||
|
//从set中删除
|
||||||
|
webSocketSet.remove(this);
|
||||||
|
//在线数减1
|
||||||
|
subOnlineCount();
|
||||||
|
//断开连接情况下,更新主板占用情况为释放
|
||||||
|
log.info("释放的sid为:"+sid);
|
||||||
|
//这里写你 释放的时候,要处理的业务
|
||||||
|
log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 收到客户端消息后调用的方法
|
||||||
|
* @ Param message 客户端发送过来的消息
|
||||||
|
*/
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message, Session session) {
|
||||||
|
log.info("收到来自窗口" + sid + "的信息:" + message);
|
||||||
|
//群发消息
|
||||||
|
for (WebSocketServer item : webSocketSet) {
|
||||||
|
try {
|
||||||
|
item.sendMessage(message);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Param session
|
||||||
|
* @ Param error
|
||||||
|
*/
|
||||||
|
@OnError
|
||||||
|
public void onError(Session session, Throwable error) {
|
||||||
|
log.error("发生错误");
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实现服务器主动推送
|
||||||
|
*/
|
||||||
|
public void sendMessage(String message) throws IOException {
|
||||||
|
this.session.getBasicRemote().sendText(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 群发自定义消息
|
||||||
|
*/
|
||||||
|
public static void sendInfo(String message, @PathParam("sid") String sid) throws IOException {
|
||||||
|
log.info("推送消息到窗口" + sid + ",推送内容:" + message);
|
||||||
|
for (WebSocketServer item : webSocketSet) {
|
||||||
|
try {
|
||||||
|
//这里可以设定只推送给这个sid的,为null则全部推送
|
||||||
|
if (sid == null) {
|
||||||
|
//item.sendMessage(message);
|
||||||
|
} else if (item.sid.equals(sid)) {
|
||||||
|
item.sendMessage(message);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized int getOnlineCount() {
|
||||||
|
return onlineCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized void addOnlineCount() {
|
||||||
|
WebSocketServer.onlineCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized void subOnlineCount() {
|
||||||
|
WebSocketServer.onlineCount--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() {
|
||||||
|
return webSocketSet;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user