引入webSocket测试连接成功,后续在此基础上修改
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package com.klp.web.controller.websocket;
|
||||||
|
|
||||||
|
import com.klp.common.core.controller.BaseController;
|
||||||
|
import com.klp.common.core.domain.R;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebSocket测试控制器
|
||||||
|
*
|
||||||
|
* @author klp
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/websocket/test")
|
||||||
|
public class WebSocketTestController extends BaseController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试WebSocket端点是否可用
|
||||||
|
*/
|
||||||
|
@GetMapping("/status")
|
||||||
|
public R<String> getWebSocketStatus() {
|
||||||
|
return R.ok("WebSocket端点可用,连接地址: ws://localhost:8080/websocket/message");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前连接数
|
||||||
|
*/
|
||||||
|
@GetMapping("/connections")
|
||||||
|
public R<Integer> getConnectionCount() {
|
||||||
|
// 这里可以调用WebSocketUsers.getUsers().size()来获取实际连接数
|
||||||
|
return R.ok(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -140,6 +140,9 @@ security:
|
|||||||
# 扫码枪获取出入库单详情
|
# 扫码枪获取出入库单详情
|
||||||
- /wms/stockIoDetail
|
- /wms/stockIoDetail
|
||||||
- /wms/stockIo/scanInStock
|
- /wms/stockIo/scanInStock
|
||||||
|
# WebSocket路径
|
||||||
|
- /websocket/**
|
||||||
|
- /wms/websocket/**
|
||||||
|
|
||||||
|
|
||||||
# MyBatisPlus配置
|
# MyBatisPlus配置
|
||||||
|
|||||||
@@ -66,7 +66,10 @@
|
|||||||
<groupId>com.klp</groupId>
|
<groupId>com.klp</groupId>
|
||||||
<artifactId>klp-common</artifactId>
|
<artifactId>klp-common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.klp.framework.websocket;
|
||||||
|
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 信号量相关处理
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class SemaphoreUtils
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* SemaphoreUtils 日志控制器
|
||||||
|
*/
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(SemaphoreUtils.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取信号量
|
||||||
|
*
|
||||||
|
* @param semaphore
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean tryAcquire(Semaphore semaphore)
|
||||||
|
{
|
||||||
|
boolean flag = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
flag = semaphore.tryAcquire();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LOGGER.error("获取信号量异常", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 释放信号量
|
||||||
|
*
|
||||||
|
* @param semaphore
|
||||||
|
*/
|
||||||
|
public static void release(Semaphore semaphore)
|
||||||
|
{
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
semaphore.release();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
LOGGER.error("释放信号量异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.klp.framework.websocket;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* websocket 配置
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class WebSocketConfig
|
||||||
|
{
|
||||||
|
@Bean
|
||||||
|
public ServerEndpointExporter serverEndpointExporter()
|
||||||
|
{
|
||||||
|
return new ServerEndpointExporter();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package com.klp.framework.websocket;
|
||||||
|
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
import javax.websocket.OnClose;
|
||||||
|
import javax.websocket.OnError;
|
||||||
|
import javax.websocket.OnMessage;
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* websocket 消息处理
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ServerEndpoint("/websocket/message")
|
||||||
|
public class WebSocketServer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* WebSocketServer 日志控制器
|
||||||
|
*/
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServer.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认最多允许同时在线人数100
|
||||||
|
*/
|
||||||
|
public static int socketMaxOnlineCount = 100;
|
||||||
|
|
||||||
|
private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接建立成功调用的方法
|
||||||
|
*/
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session) throws Exception
|
||||||
|
{
|
||||||
|
boolean semaphoreFlag = false;
|
||||||
|
// 尝试获取信号量
|
||||||
|
semaphoreFlag = SemaphoreUtils.tryAcquire(socketSemaphore);
|
||||||
|
if (!semaphoreFlag)
|
||||||
|
{
|
||||||
|
// 未获取到信号量
|
||||||
|
LOGGER.error("\n 当前在线人数超过限制数- {}", socketMaxOnlineCount);
|
||||||
|
WebSocketUsers.sendMessageToUserByText(session, "当前在线人数超过限制数:" + socketMaxOnlineCount);
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 添加用户
|
||||||
|
WebSocketUsers.put(session.getId(), session);
|
||||||
|
LOGGER.info("\n 建立连接 - {}", session);
|
||||||
|
LOGGER.info("\n 当前人数 - {}", WebSocketUsers.getUsers().size());
|
||||||
|
WebSocketUsers.sendMessageToUserByText(session, "连接成功");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接关闭时处理
|
||||||
|
*/
|
||||||
|
@OnClose
|
||||||
|
public void onClose(Session session)
|
||||||
|
{
|
||||||
|
LOGGER.info("\n 关闭连接 - {}", session);
|
||||||
|
// 移除用户
|
||||||
|
boolean removeFlag = WebSocketUsers.remove(session.getId());
|
||||||
|
if (!removeFlag)
|
||||||
|
{
|
||||||
|
// 获取到信号量则需释放
|
||||||
|
SemaphoreUtils.release(socketSemaphore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抛出异常时处理
|
||||||
|
*/
|
||||||
|
@OnError
|
||||||
|
public void onError(Session session, Throwable exception) throws Exception
|
||||||
|
{
|
||||||
|
if (session.isOpen())
|
||||||
|
{
|
||||||
|
// 关闭连接
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
String sessionId = session.getId();
|
||||||
|
LOGGER.info("\n 连接异常 - {}", sessionId);
|
||||||
|
LOGGER.info("\n 异常信息 - {}", exception);
|
||||||
|
// 移出用户
|
||||||
|
WebSocketUsers.remove(sessionId);
|
||||||
|
// 获取到信号量则需释放
|
||||||
|
SemaphoreUtils.release(socketSemaphore);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务器接收到客户端消息时调用的方法
|
||||||
|
*/
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message, Session session)
|
||||||
|
{
|
||||||
|
String msg = message.replace("你", "我").replace("吗", "");
|
||||||
|
WebSocketUsers.sendMessageToUserByText(session, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
package com.klp.framework.websocket;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* websocket 客户端用户集
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class WebSocketUsers
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* WebSocketUsers 日志控制器
|
||||||
|
*/
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketUsers.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户集
|
||||||
|
*/
|
||||||
|
private static Map<String, Session> USERS = new ConcurrentHashMap<String, Session>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储用户
|
||||||
|
*
|
||||||
|
* @param key 唯一键
|
||||||
|
* @param session 用户信息
|
||||||
|
*/
|
||||||
|
public static void put(String key, Session session)
|
||||||
|
{
|
||||||
|
USERS.put(key, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除用户
|
||||||
|
*
|
||||||
|
* @param session 用户信息
|
||||||
|
*
|
||||||
|
* @return 移除结果
|
||||||
|
*/
|
||||||
|
public static boolean remove(Session session)
|
||||||
|
{
|
||||||
|
String key = null;
|
||||||
|
boolean flag = USERS.containsValue(session);
|
||||||
|
if (flag)
|
||||||
|
{
|
||||||
|
Set<Map.Entry<String, Session>> entries = USERS.entrySet();
|
||||||
|
for (Map.Entry<String, Session> entry : entries)
|
||||||
|
{
|
||||||
|
Session value = entry.getValue();
|
||||||
|
if (value.equals(session))
|
||||||
|
{
|
||||||
|
key = entry.getKey();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移出用户
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public static boolean remove(String key)
|
||||||
|
{
|
||||||
|
LOGGER.info("\n 正在移出用户 - {}", key);
|
||||||
|
Session remove = USERS.remove(key);
|
||||||
|
if (remove != null)
|
||||||
|
{
|
||||||
|
boolean containsValue = USERS.containsValue(remove);
|
||||||
|
LOGGER.info("\n 移出结果 - {}", containsValue ? "失败" : "成功");
|
||||||
|
return containsValue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取在线用户列表
|
||||||
|
*
|
||||||
|
* @return 返回用户集合
|
||||||
|
*/
|
||||||
|
public static Map<String, Session> getUsers()
|
||||||
|
{
|
||||||
|
return USERS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 群发消息文本消息
|
||||||
|
*
|
||||||
|
* @param message 消息内容
|
||||||
|
*/
|
||||||
|
public static void sendMessageToUsersByText(String message)
|
||||||
|
{
|
||||||
|
Collection<Session> values = USERS.values();
|
||||||
|
for (Session value : values)
|
||||||
|
{
|
||||||
|
sendMessageToUserByText(value, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送文本消息
|
||||||
|
*
|
||||||
|
* @param userName 自己的用户名
|
||||||
|
* @param message 消息内容
|
||||||
|
*/
|
||||||
|
public static void sendMessageToUserByText(Session session, String message)
|
||||||
|
{
|
||||||
|
if (session != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
session.getBasicRemote().sendText(message);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
LOGGER.error("\n[发送消息异常]", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOGGER.info("\n[你已离线]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
257
klp-ui/public/websocket-test.html
Normal file
257
klp-ui/public/websocket-test.html
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<title>WebSocket测试界面</title>
|
||||||
|
<script src="https://code.jquery.com/jquery-3.2.1.min.js" ></script>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
max-width: 1200px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
background: white;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.control-panel {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
padding: 15px;
|
||||||
|
background: #f8f9fa;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
.input-group {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
.input-group label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.input-group input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
.button-group {
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
.btn {
|
||||||
|
padding: 8px 16px;
|
||||||
|
margin-right: 10px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
.btn-primary {
|
||||||
|
background-color: #007bff;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.btn-success {
|
||||||
|
background-color: #28a745;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.btn-danger {
|
||||||
|
background-color: #dc3545;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.btn:disabled {
|
||||||
|
background-color: #6c757d;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
.status {
|
||||||
|
padding: 10px;
|
||||||
|
margin: 10px 0;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.status.connected {
|
||||||
|
background-color: #d4edda;
|
||||||
|
color: #155724;
|
||||||
|
border: 1px solid #c3e6cb;
|
||||||
|
}
|
||||||
|
.status.disconnected {
|
||||||
|
background-color: #f8d7da;
|
||||||
|
color: #721c24;
|
||||||
|
border: 1px solid #f5c6cb;
|
||||||
|
}
|
||||||
|
.log-area {
|
||||||
|
background: #f8f9fa;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 10px;
|
||||||
|
height: 400px;
|
||||||
|
overflow-y: auto;
|
||||||
|
font-family: 'Courier New', monospace;
|
||||||
|
font-size: 12px;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
||||||
|
.back-btn {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="back-btn">
|
||||||
|
<button class="btn btn-primary" onclick="window.location.href='/'">返回首页</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h1>WebSocket实时通信测试</h1>
|
||||||
|
|
||||||
|
<div class="control-panel">
|
||||||
|
<div class="input-group">
|
||||||
|
<label for="url">WebSocket连接地址:</label>
|
||||||
|
<input type="text" id="url" value="ws://127.0.0.1:8080/websocket/message" placeholder="输入WebSocket地址">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="button-group">
|
||||||
|
<button id="btn_join" class="btn btn-primary">连接</button>
|
||||||
|
<button id="btn_exit" class="btn btn-danger" disabled>断开</button>
|
||||||
|
<button id="btn_test_server" class="btn btn-info">测试服务器</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="status" class="status disconnected">
|
||||||
|
未连接
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="control-panel">
|
||||||
|
<div class="input-group">
|
||||||
|
<label for="message">消息内容:</label>
|
||||||
|
<textarea id="message" rows="3" placeholder="输入要发送的消息"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="button-group">
|
||||||
|
<button id="btn_send" class="btn btn-success" disabled>发送消息</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="input-group">
|
||||||
|
<label for="text_content">消息日志:</label>
|
||||||
|
<div id="text_content" class="log-area"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(document).ready(function(){
|
||||||
|
var ws = null;
|
||||||
|
|
||||||
|
// 更新状态显示
|
||||||
|
function updateStatus(connected) {
|
||||||
|
var statusDiv = document.getElementById('status');
|
||||||
|
var joinBtn = document.getElementById('btn_join');
|
||||||
|
var exitBtn = document.getElementById('btn_exit');
|
||||||
|
var sendBtn = document.getElementById('btn_send');
|
||||||
|
|
||||||
|
if (connected) {
|
||||||
|
statusDiv.textContent = '已连接';
|
||||||
|
statusDiv.className = 'status connected';
|
||||||
|
joinBtn.disabled = true;
|
||||||
|
exitBtn.disabled = false;
|
||||||
|
sendBtn.disabled = false;
|
||||||
|
} else {
|
||||||
|
statusDiv.textContent = '未连接';
|
||||||
|
statusDiv.className = 'status disconnected';
|
||||||
|
joinBtn.disabled = false;
|
||||||
|
exitBtn.disabled = true;
|
||||||
|
sendBtn.disabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加日志
|
||||||
|
function addLog(message) {
|
||||||
|
var logArea = document.getElementById('text_content');
|
||||||
|
var timestamp = new Date().toLocaleTimeString();
|
||||||
|
logArea.textContent += '[' + timestamp + '] ' + message + '\n';
|
||||||
|
logArea.scrollTop = logArea.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接
|
||||||
|
$('#btn_join').click(function() {
|
||||||
|
var url = $("#url").val();
|
||||||
|
addLog('正在连接: ' + url);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ws = new WebSocket(url);
|
||||||
|
ws.onopen = function(event) {
|
||||||
|
addLog('WebSocket连接成功!');
|
||||||
|
updateStatus(true);
|
||||||
|
}
|
||||||
|
ws.onmessage = function(event) {
|
||||||
|
addLog('收到消息: ' + event.data);
|
||||||
|
}
|
||||||
|
ws.onclose = function(event) {
|
||||||
|
addLog('WebSocket连接关闭! 代码: ' + event.code + ', 原因: ' + event.reason);
|
||||||
|
updateStatus(false);
|
||||||
|
}
|
||||||
|
ws.onerror = function(error) {
|
||||||
|
addLog('WebSocket连接错误: ' + JSON.stringify(error));
|
||||||
|
updateStatus(false);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
addLog('WebSocket连接异常: ' + error.message);
|
||||||
|
updateStatus(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 发送消息
|
||||||
|
$('#btn_send').click(function() {
|
||||||
|
var message = $('#message').val();
|
||||||
|
if (ws && ws.readyState === WebSocket.OPEN && message.trim()) {
|
||||||
|
ws.send(message);
|
||||||
|
addLog('发送消息: ' + message);
|
||||||
|
$('#message').val('');
|
||||||
|
} else {
|
||||||
|
addLog('请先连接WebSocket或输入消息内容');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 断开
|
||||||
|
$('#btn_exit').click(function() {
|
||||||
|
if (ws) {
|
||||||
|
ws.close();
|
||||||
|
ws = null;
|
||||||
|
updateStatus(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 测试服务器状态
|
||||||
|
$('#btn_test_server').click(function() {
|
||||||
|
addLog('正在测试服务器状态...');
|
||||||
|
fetch('/websocket/test/status')
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
return response.json();
|
||||||
|
} else {
|
||||||
|
throw new Error('服务器响应错误: ' + response.status);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
addLog('服务器状态: ' + data.msg);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
addLog('服务器测试失败: ' + error.message);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 回车发送消息
|
||||||
|
$('#message').keypress(function(e) {
|
||||||
|
if (e.which == 13 && !e.shiftKey) {
|
||||||
|
e.preventDefault();
|
||||||
|
$('#btn_send').click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -155,6 +155,13 @@ export default {
|
|||||||
icon: 'fas fa-cog',
|
icon: 'fas fa-cog',
|
||||||
bgColor: 'bg-indigo-500',
|
bgColor: 'bg-indigo-500',
|
||||||
link: '/system/menu'
|
link: '/system/menu'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'WebSocket测试',
|
||||||
|
description: '实时通信功能测试',
|
||||||
|
icon: 'fas fa-comments',
|
||||||
|
bgColor: 'bg-teal-500',
|
||||||
|
link: '/websocket-test.html'
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
resourceCharts: [
|
resourceCharts: [
|
||||||
@@ -216,7 +223,12 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
handleLink(item) {
|
handleLink(item) {
|
||||||
this.$router.push(item);
|
// 如果是外部链接(以http开头或.html结尾),则在新窗口打开
|
||||||
|
if (item.startsWith('http') || item.endsWith('.html')) {
|
||||||
|
window.open(item, '_blank');
|
||||||
|
} else {
|
||||||
|
this.$router.push(item);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
getGreeting() {
|
getGreeting() {
|
||||||
const hour = new Date().getHours();
|
const hour = new Date().getHours();
|
||||||
@@ -236,6 +248,7 @@ export default {
|
|||||||
case 'bg-purple-500': return 'bg-purple';
|
case 'bg-purple-500': return 'bg-purple';
|
||||||
case 'bg-red-500': return 'bg-red';
|
case 'bg-red-500': return 'bg-red';
|
||||||
case 'bg-indigo-500': return 'bg-indigo';
|
case 'bg-indigo-500': return 'bg-indigo';
|
||||||
|
case 'bg-teal-500': return 'bg-teal';
|
||||||
default: return '';
|
default: return '';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -449,6 +462,7 @@ export default {
|
|||||||
.bg-purple { background: #a855f7; }
|
.bg-purple { background: #a855f7; }
|
||||||
.bg-red { background: #ef4444; }
|
.bg-red { background: #ef4444; }
|
||||||
.bg-indigo { background: #6366f1; }
|
.bg-indigo { background: #6366f1; }
|
||||||
|
.bg-teal { background: #14b8a6; }
|
||||||
.business-module-title {
|
.business-module-title {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
|
|||||||
Reference in New Issue
Block a user