// 使用cJSON解析JSON格式的请求体
if( process_http_post_message(pss->url, pss->post_body, pss->post_body_len, &resp) == 0){
// 调试输出,查看解析后的参数
- printf("解析JSON参数: text1='%s', send='%s'\n", text1 ? text1 : "", send ? send : "");
+ printf( "process http post msg, resp[%s]\n", resp );
- /* 使用cJSON动态构建JSON响应 */
- cJSON *root = cJSON_CreateObject();
- if (root) {
- cJSON_AddStringToObject(root, "method", "POST");
- cJSON_AddStringToObject(root, "status", "success");
- cJSON_AddStringToObject(root, "message", "This is a JSON POST response");
-
- // 创建data对象
- cJSON *data = cJSON_CreateObject();
- if (data) {
- cJSON_AddStringToObject(data, "text1", text1 ? text1 : "");
- cJSON_AddStringToObject(data, "send", send ? send : "");
- cJSON_AddItemToObject(root, "data", data);
- }
- char *json_body = cJSON_PrintUnformatted(root);
+ char *json_body = resp;
json_len = json_body ? strlen(json_body) : 0;
/* 重置缓冲区指针用于HTTP头 */
if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK, "application/json",
json_len, &p, end) < 0) {
printf("设置HTTP头失败\n");
- free(text1);
- free(send);
- cJSON_Delete(root);
- if (json_body) free(json_body);
+ if (json_body) free(json_body);
return -1;
}
if (lws_finalize_write_http_header(wsi, start, &p, end) < 0) {
printf("写入HTTP头失败\n");
- free(text1);
- free(send);
- cJSON_Delete(root);
if (json_body) free(json_body);
return -1;
}
/* 写入 JSON 响应体 */
if (lws_write(wsi, (unsigned char *)json_body, json_len, LWS_WRITE_HTTP_FINAL) < 0) {
printf("写入响应体失败\n");
- free(text1);
- free(send);
- cJSON_Delete(root);
if (json_body) free(json_body);
return -1;
}
// 释放资源
free(json_body);
- cJSON_Delete(root);
- }
- // 释放解析出的参数
- free(text1);
- free(send);
} else {
/* JSON解析失败 */
printf("JSON解析失败\n");
font-size: 11px;
color: #666;
}
+ .info-tabs {
+ display: flex;
+ margin-bottom: 10px;
+ }
+ .info-tab {
+ padding: 8px 15px;
+ cursor: pointer;
+ background-color: #e0e0e0;
+ border: 1px solid #ddd;
+ border-bottom: none;
+ border-radius: 4px 4px 0 0;
+ margin-right: 5px;
+ }
+ .info-tab.active {
+ background-color: #f5f5f5;
+ font-weight: bold;
+ }
+ .info-content {
+ display: none;
+ }
+ .info-content.active {
+ display: block;
+ }
</style>
</head>
<body>
</div>
</div>
- <!-- POST 请求测试 -->
+ <!-- POST 请求测试 - 修改为获取PCS信息 -->
<div class="section">
- <h2>POST 请求测试</h2>
- <p>填写表单并提交,服务器将返回包含表单数据的 JSON 响应。</p>
-
- <div>
- <label for="post-text1">文本1:</label>
- <input type="text" id="post-text1" placeholder="输入文本1" value="测试文本">
- </div>
+ <h2>获取PCS信息测试</h2>
+ <p>点击按钮发送 POST 请求获取PCS信息,服务器将返回包含PCS详细数据的JSON响应。</p>
- <div>
- <label for="post-send">发送值:</label>
- <input type="text" id="post-send" placeholder="输入发送值" value="发送数据">
- </div>
-
- <button id="post-button" onclick="testPost()">发送 POST 请求</button>
+ <button id="post-button" onclick="getPcsInfo()">获取PCS信息</button>
<span id="post-loading" class="loading" style="display: none;">请求中...</span>
<div class="response-box" id="post-response">
xhr.send();
}
- // 只需修改testPost函数,确保它发送有效的JSON格式数据
- function testPost() {
+ // 获取PCS信息 - 按照pcs.md中的协议规范
+ function getPcsInfo() {
const postButton = document.getElementById("post-button");
const postLoading = document.getElementById("post-loading");
- const text1 = document.getElementById("post-text1").value;
- const send = document.getElementById("post-send").value;
-
- console.log("准备发送POST请求,数据:", { text1, send });
// 显示加载状态
postButton.disabled = true;
postLoading.style.display = "inline";
- showResponse("post-response", "发送 POST 请求中...");
+ showResponse("post-response", "发送获取PCS信息请求中...");
// 创建 XMLHttpRequest 对象
const xhr = new XMLHttpRequest();
- xhr.open("POST", "/api/post", true);
+ // 修改POST请求URL为pcs.md中定义的路径
+ xhr.open("POST", "/api/pcs_info", true);
// 设置内容类型为 JSON
xhr.setRequestHeader("Content-Type", "application/json");
// 设置请求完成回调
xhr.onreadystatechange = function() {
- console.log("POST请求状态改变: readyState=", xhr.readyState, ", status=", xhr.status);
if (xhr.readyState === 4) {
// 隐藏加载状态
postButton.disabled = false;
// 添加错误处理
xhr.onerror = function(error) {
- console.error("POST请求错误:", error);
+ console.error("请求错误:", error);
postButton.disabled = false;
postLoading.style.display = "none";
showResponse("post-response", "请求发生错误\n" + (error.message || "未知错误"));
// 添加超时处理
xhr.timeout = 10000; // 10秒超时
xhr.ontimeout = function() {
- console.error("POST请求超时");
+ console.error("请求超时");
postButton.disabled = false;
postLoading.style.display = "none";
showResponse("post-response", "请求超时 (超过10秒)");
};
- // 构建JSON数据
+ // 构建符合pcs.md协议规范的JSON数据
const jsonData = JSON.stringify({
- text1: text1,
- send: send
+ "code": 0,
+ "msg": "get_pcs_info"
});
console.log("发送的JSON数据:", jsonData);
addMessage("server", "WebSocket 连接已建立");
};
- // 接收消息事件 - 修复:使用专门的处理函数
+ // 接收消息事件
webSocket.onmessage = function(event) {
console.log("收到 WebSocket 消息: " + event.data);
processReceivedMessage(event.data);
}
}
- // 发送 WebSocket 消息 - 修复:统一使用webSocket变量
+ // 发送 WebSocket 消息
function sendWebSocketMessage() {
const messageInput = document.getElementById("ws-message");
const message = messageInput.value;