Browse Source

ffmpeg工具修改

master
wangwei 11 months ago
parent
commit
e1bc3eb1ea
8 changed files with 260 additions and 229 deletions
  1. +2
    -2
      loadCmd.properties
  2. +5
    -3
      src/main/java/com/github/bluesbruce/ffch/handler/DefaultOutHandlerMethod.java
  3. +3
    -1
      src/main/java/com/github/bluesbruce/ffch/handler/OutHandler.java
  4. +60
    -5
      src/main/java/com/github/bluesbruce/spring/service/FFrtmpServer.java
  5. +2
    -2
      src/main/java/com/github/bluesbruce/spring/service/MqttLiveHandle.java
  6. +134
    -18
      src/main/java/com/github/bluesbruce/spring/service/RtmpLiveService.java
  7. +0
    -34
      src/main/java/com/github/bluesbruce/spring/web/SubController.java
  8. +54
    -164
      src/main/webapp/push.jsp

+ 2
- 2
loadCmd.properties View File

@@ -4,11 +4,11 @@ pushUrl=rtmp://192.168.10.101:19350/rlive/stream_11?sign=rHtBg3sz
#playUrl=http://192.168.10.101:18000/flv/live/34020000001110000001_34020000001320000099_0200000099.flv
playUrl=http://192.168.10.101:18000/flv/live/stream_176.flv
#固定通道 1 获取通道 2 多线程双路 3 单线程多路 4
type=4
type=3

mqttUrl=tcp://106.15.120.154
#主题 12345替换对应机场编号 004替换控制板id
mqttTopic=/v1/12345/rtmp/live,v1/004/confirm/DroneBMS
mqttTopic=/v1/12345936/rtmp/live,v1/004/confirm/DroneBMS

#推流时长(毫秒)1000表示1秒
time=600000

+ 5
- 3
src/main/java/com/github/bluesbruce/ffch/handler/DefaultOutHandlerMethod.java View File

@@ -2,6 +2,9 @@ package com.github.bluesbruce.ffch.handler;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
* 默认任务消息输出处理
* @author eguid
@@ -15,7 +18,7 @@ public class DefaultOutHandlerMethod implements OutHandlerMethod{
* 任务是否异常中断,如果
*/
public boolean isBroken=false;
public static Map outIdMap = new HashMap();
@Override
public void parse(String id,String msg) {
long begin = System.currentTimeMillis();
@@ -40,9 +43,8 @@ public class DefaultOutHandlerMethod implements OutHandlerMethod{
}else {
isBroken=false;
log.info(id + "消息:" + msg);
}
outIdMap.put(id,msg);
}

@Override

+ 3
- 1
src/main/java/com/github/bluesbruce/ffch/handler/OutHandler.java View File

@@ -27,6 +27,7 @@ public class OutHandler extends Thread {
/**消息处理方法*/
private OutHandlerMethod ohm;
public static String outMsg;

/**
* 创建输出线程(默认立即开启线程)
@@ -93,9 +94,10 @@ public class OutHandler extends Thread {
try {
if (CommandManager.config.isDebug()) {
log.info(id + "开始推流!");
}
}
while (desstatus && (msg = br.readLine()) != null) {
ohm.parse(id,msg);
outMsg=msg;
if(ohm.isbroken()) {
log.info("检测到中断,提交重启任务给保活处理器");
//如果发生异常中断,立即进行保活

+ 60
- 5
src/main/java/com/github/bluesbruce/spring/service/FFrtmpServer.java View File

@@ -4,13 +4,19 @@ package com.github.bluesbruce.spring.service;
import com.github.bluesbruce.ffch.CommandManager;
import com.github.bluesbruce.ffch.CommandManagerImpl;
import com.github.bluesbruce.ffch.commandbuidler.CommandBuidlerFactory;
import com.github.bluesbruce.ffch.data.CommandTasker;
import com.github.bluesbruce.ffch.handler.OutHandler;
import com.github.bluesbruce.spring.config.CmdParam;
import lombok.extern.slf4j.Slf4j;
import org.h2.util.StringUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Iterator;

import static com.github.bluesbruce.ffch.util.PropertiesUtil.load;

@Slf4j
@@ -19,17 +25,18 @@ import static com.github.bluesbruce.ffch.util.PropertiesUtil.load;
public class FFrtmpServer implements ApplicationRunner {

public static final CmdParam cmdParam=load("/loadCmd.properties", CmdParam.class);
CommandManager manager = new CommandManagerImpl();
int status=0;
@Override
public void run(ApplicationArguments args) {
try {
/*CommandManager manager = new CommandManagerImpl();
// -rtsp_transport tcp
//测试多个任何同时执行和停止情况
//false表示使用配置文件中的ffmpeg路径,true表示本条命令已经包含ffmpeg所在的完整路径
//manager.start("tomcat", "ffmpeg -i http://192.168.10.101:18000/flv/live/34020000001110000002_34020000001320000071_0200000071.flv -vcodec copy -acodec copy -f flv -y rtmp://192.168.10.101:19350/rlive/stream_9?sign=f8a15b6n",false);
//manager.start("tomcat", "ffmpeg -i rtsp://192.168.144.25:554/stream=0 -vcodec copy -acodec copy -f flv -y rtmp://221.226.114.142:19350/rlive/stream_9?sign=f8a15b6n",false);
//manager.start("tomcat1", "ffmpeg -i rtsp://192.168.144.25:554/stream=0 -vcodec copy -acodec copy -f flv -y rtmp://221.226.114.142:19350/rlive/stream_11?sign=rHtBg3sz",false);
if (cmdParam.getType()==1) {
/*if (cmdParam.getType()==1) {
manager.start("test0", CommandBuidlerFactory.createBuidler()
.add("ffmpeg")
.add("-rtsp_transport","tcp").add("-i", cmdParam.getPlayUrl())
@@ -66,7 +73,7 @@ public class FFrtmpServer implements ApplicationRunner {
//单进程 多路
manager.start("test4", CommandBuidlerFactory.createBuidler()
.add("ffmpeg")
*//*.add("-rtsp_transport", "tcp")*//*.add("-i", cmdParam.getPlayUrl())
.add("-rtsp_transport", "tcp").add("-i", cmdParam.getPlayUrl())
.add("-vcodec", "copy")
.add("-acodec", "copy")
.add("-b:v", "2M")
@@ -76,15 +83,63 @@ public class FFrtmpServer implements ApplicationRunner {
.add("-map", "0")
.add("-f").add(cmdParam.getPushUrlMap()));
}
Thread.sleep(cmdParam.getTime());
status =1;
//检测线程
Thread thread = new Thread(new Runnable() {
public void run() {
try {
checkMsg();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
Thread.sleep(20000);
// 停止全部任务
manager.stopAll();*/
manager.stopAll();
status=0;*/
}catch (Exception e){
log.error("",e);
}
//ds.close();
}

private void checkMsg() throws InterruptedException {
long oldtime = System.currentTimeMillis();
String oldMsg = "";
while (true) {
String msg = OutHandler.outMsg==null?"":OutHandler.outMsg;
if (!msg.equals(oldMsg)) {
oldMsg=msg;
}
if (System.currentTimeMillis()-oldtime>15000&&msg.equals(oldMsg)&&status==1){
log.info("消息未输出{}",msg);
if(manager.queryAll().size()>0){
Collection<CommandTasker> list = manager.queryAll();
Iterator<CommandTasker> commandTaskerIterator = list.iterator();
CommandTasker s = commandTaskerIterator.next();
log.info("0000{},{}",s.getId(),s.getCommand());
log.info("任务消息未输出",s);
manager.stopAll();
Thread.sleep(2000);
if (status==0){
return;
}
String result= manager.start(s.getId(),s.getCommand().split("bin/")[1]);
if (!StringUtils.isNullOrEmpty(result)){
oldtime= System.currentTimeMillis();
}else{
log.info("推流失败,重新推流");
}
};
}
if (status==0){
return;
}
Thread.sleep(2000);
}
}


}

+ 2
- 2
src/main/java/com/github/bluesbruce/spring/service/MqttLiveHandle.java View File

@@ -21,7 +21,7 @@ public class MqttLiveHandle {
if (!ObjectUtils.isEmpty(jsonObject.get("command"))) {
String cmdoperat = jsonObject.get("command").toString();
if (cmdoperat.equals("start")) {
Thread.sleep(15000);
Thread thread = new Thread(new Runnable() {
String code = jsonObject.get("code") == null ? topic.split("/")[2] : jsonObject.get("code").toString();
public void run() {
@@ -45,7 +45,7 @@ public class MqttLiveHandle {
String boolbms = cmdoperat.getString("Power");
log.info("boolbms:",jsonObject.get("Power"));
if (boolbms.equals("on")) {
Thread.sleep(15000);
Thread thread = new Thread(new Runnable() {
String code = jsonObject.get("code") == null ? topic.split("/")[2] : jsonObject.get("code").toString();
public void run() {

+ 134
- 18
src/main/java/com/github/bluesbruce/spring/service/RtmpLiveService.java View File

@@ -5,6 +5,8 @@ import com.github.bluesbruce.ffch.CommandManagerImpl;
import com.github.bluesbruce.ffch.commandbuidler.CommandBuidlerFactory;
import com.github.bluesbruce.ffch.data.CommandTasker;
import com.alibaba.fastjson.JSONObject;
import com.github.bluesbruce.ffch.handler.DefaultOutHandlerMethod;
import com.github.bluesbruce.ffch.handler.OutHandler;
import com.github.bluesbruce.spring.config.CmdParam;
import com.github.bluesbruce.spring.mqttService.HttpURLConnectionUtil;
import com.github.bluesbruce.spring.mqttService.send.MqttProviderConfig;
@@ -14,8 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.List;
import java.util.*;

import static com.github.bluesbruce.ffch.util.PropertiesUtil.load;

@@ -30,7 +31,9 @@ public class RtmpLiveService {
@Autowired
private MqttProviderConfig mqttProviderConfig;

public static final CommandManager manager = new CommandManagerImpl();
public static int status =0;

public static CommandManager manager = new CommandManagerImpl();

/**
* h获取通道并推送
@@ -51,6 +54,18 @@ public class RtmpLiveService {
log.info("任务运行中");
return;
}
//检测线程
Thread thread = new Thread(new Runnable() {
public void run() {
try {
checkMsg();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
status=1;
//Thread.sleep(5000);
String pushUrl = "";
if (cmdParam.getType() == 2) {
@@ -80,42 +95,42 @@ public class RtmpLiveService {
} else if (cmdParam.getType() == 4) {
runRtmp1to2(cmdParam.getPlayUrl());
}
status=0;
DefaultOutHandlerMethod.outIdMap=new HashMap();
}

private void runRtmp2to2(String playUrl) throws InterruptedException {
try {
String reTopic = cmdParam.getMqttTopic().replace("live", "result");
String taskId = manager.start("test1", CommandBuidlerFactory.createBuidler()
String taskId = manager.start("push3-1", CommandBuidlerFactory.createBuidler()
.add("ffmpeg")
.add("-rtsp_transport", "tcp").add("-i", playUrl)
.add("-vcodec", "copy")
/*.add("-acodec", "copy")*/
.add("-f", "flv")
.add("-b:v", "2M")
.add("-maxrate", "2M")
.add("-b:v", "1M")
.add("-maxrate", "1M")
.add("-bufsize", "1M")
.add("-y").add(cmdParam.getPushUrl()));
Thread.sleep(3000);
String taskId2 = manager.start("test2", CommandBuidlerFactory.createBuidler()
.add("-an").add(cmdParam.getPushUrl()));
Thread.sleep(5000);
String taskId2 = manager.start("push3-2", CommandBuidlerFactory.createBuidler()
.add("ffmpeg")
.add("-rtsp_transport", "tcp").add("-i", playUrl)
.add("-vcodec", "copy")
/* .add("-acodec", "copy")*/
.add("-f", "flv")
.add("-b:v", "2M")
.add("-maxrate", "2M")
.add("-b:v", "1M")
.add("-maxrate", "1M")
.add("-bufsize", "1M")
.add("-y").add(cmdParam.getPushUrl2()));
.add("-an").add(cmdParam.getPushUrl2()));
JSONObject jsonObject = new JSONObject();
if (StringUtils.isNullOrEmpty(taskId) && StringUtils.isNullOrEmpty(taskId2)) {
jsonObject.put("code", -1);
jsonObject.put("msg", "推流服务失败"); //推流失败
mqttProviderConfig.publish(2, false, reTopic, jsonObject.toJSONString());
// 停止全部任务
manager.stopAll();
return;
/*manager.stopAll();
return;*/
} else {
jsonObject.put("code", 0);
jsonObject.put("msg", "推流服务启动成功");
@@ -135,7 +150,7 @@ public class RtmpLiveService {
try {
//单进程 多路
String reTopic = cmdParam.getMqttTopic().replace("live", "result");
String taskId = manager.start("test4", CommandBuidlerFactory.createBuidler()
String taskId = manager.start("push4", CommandBuidlerFactory.createBuidler()
.add("ffmpeg")
.add("-rtsp_transport", "tcp").add("-i", playUrl)
.add("-vcodec", "copy")
@@ -218,7 +233,7 @@ public class RtmpLiveService {
try {
log.info("获取播流地址:{}");
//CommandManager manager = new CommandManagerImpl();
String taskId = manager.start(code, CommandBuidlerFactory.createBuidler()
String taskId = manager.start("push1", CommandBuidlerFactory.createBuidler()
.add("ffmpeg").add("-rtsp_transport", "tcp")
.add("-i", playUrl)
.add("-vcodec", "copy")
@@ -258,8 +273,109 @@ public class RtmpLiveService {
//ds.close();
}

private void checkMsg1() throws InterruptedException {
long oldtime = System.currentTimeMillis();
String oldMsg = "";
while (true) {
String msg = OutHandler.outMsg==null?"":OutHandler.outMsg;
if (!msg.equals(oldMsg)) {
oldMsg=msg;
oldtime=System.currentTimeMillis();
}
log.info("消息输出{},状态{}",msg,status);
if (System.currentTimeMillis()-oldtime>15000&&msg.equals(oldMsg)&&status==1){
//log.info("消息未输出{}",msg);
if(manager.queryAll().size()>0){
Collection<CommandTasker> list = manager.queryAll();
Iterator<CommandTasker> commandTaskerIterator = list.iterator();
CommandTasker s = commandTaskerIterator.next();
log.info("0000{},{}",s.getId(),s.getCommand());
log.info("任务消息未输出",s);
manager.stop(s.getId());
Thread.sleep(2000);
String result= manager.start(s.getId(),s.getCommand().split("bin/")[1]);
if (!StringUtils.isNullOrEmpty(result)){
oldtime= System.currentTimeMillis();
}else{
log.info("推流失败,重新推流");
}
};
}
if (status==0){
break;
}
Thread.sleep(200);
}
}
private void checkMsg() throws InterruptedException {
long oldtime = System.currentTimeMillis();
String oldMsg = "";
long oldtime2 = System.currentTimeMillis();
String oldMsg2 = "";
while (true) {
Map map = DefaultOutHandlerMethod.outIdMap;
if (cmdParam.getType()==3){
Map map1 = checkMsgDetil(oldtime,oldMsg,map.get("push3-1")==null?"":map.get("push3-1").toString(),"push3-1");
oldMsg=map1.get("oldMsg")==null?"":map1.get("oldMsg").toString();
oldtime=Long.parseLong(map1.get("oldtime")==null?"0":map1.get("oldtime").toString());
Map map2 = checkMsgDetil(oldtime2,oldMsg2,map.get("push3-2")==null?"":map.get("push3-2").toString(),"push3-2");
oldMsg2=map2.get("oldMsg")==null?"":map2.get("oldMsg").toString();
oldtime2=Long.parseLong(map2.get("oldtime")==null?"0":map2.get("oldtime").toString());
}else {
String msg = "";
String keys ="";
for (Object key : map.keySet()) {
msg=map.get(key).toString();
if (!(key.toString().equals("push3-1")||key.toString().equals("push3-1"))) {
keys = key.toString();
}
}
Map map1 = checkMsgDetil(oldtime,oldMsg,msg,keys);
oldMsg=map1.get("oldMsg")==null?"":map1.get("oldMsg").toString();
oldtime=Long.parseLong(map1.get("oldtime")==null?"0":map1.get("oldtime").toString());
}
if (status==0){
break;
}
Thread.sleep(200);
}
}

public Map checkMsgDetil(long oldtime,String oldMsg,String msg,String id) throws InterruptedException {
if (!msg.equals(oldMsg)) {
oldMsg = msg;
oldtime = System.currentTimeMillis();
}
log.info("消息输出{},状态{}", msg, status);
if (System.currentTimeMillis() - oldtime > 15000 && msg.equals(oldMsg) && status == 1) {
//log.info("消息未输出{}",msg);
/*Collection<CommandTasker> list = manager.queryAll();
Iterator<CommandTasker> commandTaskerIterator = list.iterator();*/
CommandTasker s = manager.query(id);
if (!ObjectUtils.isEmpty(s)) {
log.info("0000{},{}", s.getId(), s.getCommand());
log.info("任务消息未输出", s);
manager.stop(s.getId());
Thread.sleep(2000);
String result = manager.start(s.getId(), s.getCommand().split("bin/")[1]);
if (!StringUtils.isNullOrEmpty(result)) {
oldtime = System.currentTimeMillis();
} else {
log.info("推流失败,重新推流");
}
}

}
Map map = new HashMap();
map.put("oldtime",oldtime);
map.put("oldMsg",oldMsg);
return map;
}

public void stopRtmp() {
try {
status=0;
DefaultOutHandlerMethod.outIdMap=new HashMap();
log.info("停止推流");
//CommandManager manager = new CommandManagerImpl();
// 停止全部任务

+ 0
- 34
src/main/java/com/github/bluesbruce/spring/web/SubController.java View File

@@ -1,34 +0,0 @@
package com.github.bluesbruce.spring.web;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class SubController {
@Resource
private ObjectMapper objectMapper;

@Value("${spring.mqtt.client.id}")
private String clientId;

@Value("${spring.mqtt.default.topic}")
private String defaultTopic;

@RequestMapping("/init")
@ResponseBody
public String subject(String topic, int qos) {
try {
return "发送成功";
} catch (Exception e) {
e.printStackTrace();
return "发送失败";
}
}

}

src/main/webapp/sub.jsp → src/main/webapp/push.jsp View File

@@ -9,7 +9,7 @@
<meta name="author" content="https://gitee.com/PirateFlag">

<link rel="icon" href="">
<title>mqtt工具</title>
<title>推流工具</title>

<!-- 引入 Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
@@ -74,24 +74,25 @@
<!--主要介绍-->
<div class="container-fluid" style="height: 150px;">
<div class="row" style="margin-top: 25px">
<div class="col-md-6">
<%--<div class="col-md-6">
<select type="" class="form-control" id="selectV" >
<option value="0">请选择</option>
<%--<option value="tcp://106.15.120.154:1883">测试环境</option>
<option value="tcp://192.168.11.22:1883">开发环境</option>--%>
&lt;%&ndash;<option value="tcp://106.15.120.154:1883">测试环境</option>
<option value="tcp://192.168.11.22:1883">开发环境</option>&ndash;%&gt;
</select>

</div>
<div class="col-md-3">
</div>--%>
<%--<div class="col-md-3">
<input type="text" class="form-control" id="clientId" placeholder="请输入客户端id">
</div>
<button type="" class="btn btn-success" onclick="selectV()" style="">切换</button>
<button type="" class="btn btn-success" onclick="selectV()" style="">切换</button>--%>

<label class="remoteclass" style="margin-left: 20%;font-size: 22px">kaifa</label>
<label class="remoteclass" style="margin-left: 10%;font-size: 22px;color: #010101">推流服务状态:</label>
<label class="tstatus" style="margin-left: 1%;font-size: 12px;color: green">..</label>
<%--<a href="#" class="btn btn-primary my-2">主要跳转按钮</a>
<a href="#" class="btn btn-warning my-2">次要跳转按钮</a>--%>
</div>
<div class="row" style="margin-top: 25px">
<%--<div class="row" style="margin-top: 25px">
<div class="col-md-6">
<input type="text" class="form-control" id="subAdd1" placeholder="请输入订阅">
</div>
@@ -100,10 +101,18 @@
</div>
<button type="" class="btn btn-success" onclick="subAdd1()" style="">提交</button>

<button type="" class="btn btn-success" onclick="loadSubList()" style="margin-left: 20%">订阅列表</button>
<%--<a href="#" class="btn btn-primary my-2">主要跳转按钮</a>
<a href="#" class="btn btn-warning my-2">次要跳转按钮</a>--%>
</div>
&lt;%&ndash;<button type="" class="btn btn-success" onclick="loadSubList()" style="margin-left: 20%">订阅列表</button>&ndash;%&gt;
&lt;%&ndash;<a href="#" class="btn btn-primary my-2">主要跳转按钮</a>
<a href="#" class="btn btn-warning my-2">次要跳转按钮</a>&ndash;%&gt;
</div>--%>
<div class="row" style="margin-right: 25px">
<div class="col-md-1" style="margin-left: 70%">
<button type="" class="btn btn-success" onclick="restart()" >重新推流</button>
</div>
<div class="col-md-1">
<button type="" class="btn btn-danger" onclick="stop()" >停止推流</button>
</div>
</div>
</div>

<div class="" >
@@ -111,16 +120,16 @@

<!--单个流卡-->
<div class="row">
<div class="col-md-6">
<div class="col-md-12">
<!--使用card式布局-->
<div class="card mb-6 box-shadow">
<div class="card mb-12 box-shadow">
<div class="card body">
<%--<h5 class="card-title">订阅</h5>--%>
<a href="#" onclick="subAll()" class="btn btn-success my-2" style="float: right">全部订阅</a>
<%--<a href="#" onclick="subAll()" class="btn btn-success my-2" style="float: right">全部订阅</a>--%>
<div class="d-flex justify-content-between aligin-item-center">
<table id="reportTable1" class="table table-bordered table-condensed">
<thead>
<th width="10%" style="display:none;">id</th><th width="10%">排序</th><th width="10%">订阅</th><th width="10%">消息</th><th width="10%">备注</th><th width="10%" colspan="2">操作</th>
<th width="10%" style="display:none;">id</th><th width="10%">任务名称</th><th width="60%">指令</th><th width="10%">备注</th><th width="10%" colspan="2">操作</th>
</thead>
<tbody id="subTr">
</tbody>
@@ -199,27 +208,16 @@
</div>

<script>

$(function () {
sub();
indexMsg();
remoteloadSelect();
remote()
/*var s="{\n" +
" \"mid\": 1000000000020033,\n" +
" \"deviceid\": \"1000000001154323\",\n" +
" \"timestamp\": 1652248505,\n" +
" \"param\": {\n" +
" \"Action\": 0\n" +
" }\n" +
" }";
$("pre").html(s);*/
window.setInterval(indexMsg,2000)

})
function subAll(){
//重新推流
function restart(){
$.ajax({
type:"get",
url:"/subAll",
url:"/start",
//data:{"checkName":$("#userName").val()}, //以键值对的方式传数据到后台 Servlet
dataType:"json",
success:function(mydata){
@@ -232,156 +230,48 @@
}
});
}
function sub() {
//停止推流
function stop(){
$.ajax({
type:"get",
url:"/indexsub",
data:{"checkName":$("#userName").val()}, //以键值对的方式传数据到后台 Servlet
url:"/stop",
//data:{"checkName":$("#userName").val()}, //以键值对的方式传数据到后台 Servlet
dataType:"json",
success:function(mydata){
// alert(mydata.find); //获取回调值mydata中数据:mydata.键《find》------>的值表示方法 该回调函数值mydata可以是JSON格式的:对象或者数组
if (mydata=="success") {
alert("操作成功!"); //获取回调值mydata中数据:mydata.键《find》------>的值表示方法 该回调函数值mydata可以是JSON格式的:对象或者数组
// console.log(mydata.find);
if(mydata!==null){
var str ="";
for (var i = 0; i <mydata.length ; i++) {

var ex01=mydata[i].ex01==null?"":mydata[i].ex01;
var ex02=mydata[i].ex02==null?"":mydata[i].ex02;
var ex03=mydata[i].ex03==null?"":mydata[i].ex03;
str+="<tr class=\"info\"><td style='display: none'>"+mydata[i].id+"</td>" +
"<td style='background-color: #d3e4db' ondblclick='msgClick(this)'><label >"+ex03+"</label>" +
"<input class='tdedit' onchange='msgvalue(this)' onblur='ex02value(this)' type='text' style='display: none' ></td>" +
"<td>"+mydata[i].sub+"</td>" +
"<td style='background-color: #d3e4db' ondblclick='msgClick(this)'><label >"+ex01+"</label>" +
"<input class='tdedit' onchange='msgvalue(this)' onblur='msgvalue(this)' type='text' style='display: none' ></td>" +
"<td style='background-color: #c9d4e4' ondblclick='msgClick(this)'><label >"+ex02+"</label >" +
"<input class='tdedit' onchange='ex02value(this)' onblur='ex02value(this)' type='text' style='display: none' ></td>" +
"<td><button class='btn-success' onclick='subsave(this)'>保存</button><button class='btn-danger' onclick='delsave(this)'>删除</button></td>" +
"<td><button class='btn-info' onclick='subsub(this)'>发送</button></td>" +
"</tr>";
}
$("#subTr").html(str);
}else{
alert("操作失败!");
}
}
});
}

function msgClick(obj) {
//if($(this).find("input").css("display")=="none"){
$(obj).find("label").text("");
$(obj).find("input").css("display","");
// }
}

function msgvalue(obj) {
$(obj).hide();
$(obj).closest("td").find("label").text($(obj).val())
}

function ex02value(obj) {
$(obj).hide();
$(obj).closest("td").find("label").text($(obj).val())
}

function subsub(obj) {
var sub= $(obj).closest("tr").find("td").eq(2).text();
var ex01= $(obj).closest("tr").find("td").eq(3).find("label").text();
function indexMsg() {
$.ajax({
type: "post",
url: "/sendMessage",
data: JSON.stringify({ "sub":sub,"ex01":ex01}), //以键值对的方式传数据到后台 Servlet
url: "/status",
dataType: "json",
contentType: 'application/json',
cache: false,
success: function (mydata) {
if (mydata=="success"){
alert("操作成功!")
}
}
})
}

$(".tstatus").html(mydata.msg);
if (mydata.status==1) {
var ex01 = mydata.name == null ? "" : mydata.name;
var ex02 = mydata.cmd == null ? "" : mydata.cmd;
var str="<tr class=\"info\">" +
"<td style='background-color: #d3e4db'><label >"+ex01+"</label>" +
"<td style='background-color: #d3e4db'><label >"+ex02+"</label>" +
"<td style='background-color: #d3e4db'><label ></label>" +
/*"<td><button class='btn-success' onclick='subsave(this)'>保存</button><button class='btn-danger' onclick='delsave(this)'>删除</button></td>" +*/
"</tr>";
$("#subTr").html(str);
}else {
$("#subTr").html("");
}

function subsave(obj) {
var id= $(obj).closest("tr").find("td").eq(0).text();
var ex03= $(obj).closest("tr").find("td").eq(1).find("label").text();
var sub= $(obj).closest("tr").find("td").eq(2).text();
var ex01= $(obj).closest("tr").find("td").eq(3).find("label").text();
var ex02= $(obj).closest("tr").find("td").eq(4).find("label").text();

$.ajax({
type: "post",
url: "/editsub",
data: JSON.stringify({"id": id, "sub":sub,"ex01":ex01,"ex02":ex02,"ex03":ex03}), //以键值对的方式传数据到后台 Servlet
dataType: "json",
contentType: 'application/json',
cache: false,
success: function (mydata) {
if (mydata=="success"){
alert("操作成功!")
}
}
})
}
function delsave(obj) {
var id= $(obj).closest("tr").find("td").eq(0).text();
var ex03= $(obj).closest("tr").find("td").eq(1).find("label").text();
var sub= $(obj).closest("tr").find("td").eq(2).text();
var ex01= $(obj).closest("tr").find("td").eq(3).find("label").text();
var ex02= $(obj).closest("tr").find("td").eq(4).find("label").text();

$.ajax({
type: "post",
url: "/delsub",
data: JSON.stringify({"id": id, "sub":sub,"ex01":ex01,"ex02":ex02,"ex03":ex03}), //以键值对的方式传数据到后台 Servlet
dataType: "json",
contentType: 'application/json',
cache: false,
success: function (mydata) {
if (mydata=="success"){
alert("操作成功!")
window.location.reload();
}
}
})
}
function subAdd1() {
if ($("#subAdd1").val().trim()=="") {
alert("不能为空!")
return;
}
$.ajax({
type: "post",
url: "/addsub1",
data: JSON.stringify({"sub":$("#subAdd1").val(),"ex03":$("#pxAdd1").val()}), //以键值对的方式传数据到后台 Servlet
dataType: "json",
contentType: 'application/json',
cache: false,
success: function (mydata) {
if (mydata=="success"){
alert("操作成功!")
window.location.reload();
}
}
})
}
function indexMsg() {
$.ajax({
type: "post",
url: "/indexMsg",
dataType: "json",
contentType: 'application/json',
cache: false,
success: function (mydata) {
var divmsg="";
if (mydata.length>0){
for (var i = 0; i < mydata.length; i++) {
var msg=mydata[i].msg;
divmsg+="<div style=\"background-color: #a8c8fc;border: 1px solid #000;\" class=\"col-md-12\"><span style='font-weight: bold'>"+mydata[i].sub+"</span><pre style='white-space: pre-wrap;background-color: #a8c8fc'>"+mydata[i].msg+"</pre><span>"+mydata[i].time+"</span>" +
"<span style='float: right'>"+mydata[i].ex01+"</span></div>";
}
$(".classmsg").html(divmsg);
}
}
})
}

Loading…
Cancel
Save