创新实训———通过socket实现项目集成

项目的最后阶段,我们小组成员面对面进行了项目集成方面的工作,顾名思义就是将我们的人脸识别系统的前端后台,各个部分进行衔接,最后得到一个完整的项目。


对于项目中不同语言间的通信,我们选择了socket编程接口进行衔接。

下面是php端的socket通信实现代码:

        $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ) or die ( 'could not create socket' );
        $connect = socket_connect ( $socket, '192.168.2.146', 6666 );
        socket_write ( $socket, '2-是否结束' ."\n");
        $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );

        socket_close($socket);

        return $str;

在TP5.0框架下,我将以上实现写成了一系列函数,用于向端口另一端的java函数传递不同的指令,并且围绕这些方法设计了一系列相关的实现。

php:

public function socketc(){//查询录制是否结束

        $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ) or die ( 'could not create socket' );
        $connect = socket_connect ( $socket, '192.168.2.146', 6666 );
        socket_write ( $socket, '3' . "\n" );
        $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );

        socket_close($socket);

        $s=date("h:i:sa");

        $this->assign("state",$s);

        return $s;

    }

    public function sockets(){//录制开始

        $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ) or die ( 'could not create socket' );
        $connect = socket_connect ( $socket, '192.168.2.146', 6666 );
        socket_write ( $socket, '1' . "\n" );
        $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );

        socket_close($socket);

        return $str;

    }

    public function socketf(){//录制结束

        $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ) or die ( 'could not create socket' );
        $connect = socket_connect ( $socket, '192.168.2.146', 6666 );
        socket_write ( $socket, '2'."\n" );
        $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );

        socket_close($socket);

        return $str;

    }

    public function u_sockets($i,$st,$ft){//上传开始

        $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ) or die ( 'could not create socket' );
        $connect = socket_connect ( $socket, '192.168.2.146', 6666 );
        $s="1&".$i."&".$st."&".$ft."";
        socket_write ( $socket, $s."\n");
        $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );

        socket_close($socket);

        return $str;

    }

    public function u_socketc(){//查询处理是否结束

        $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ) or die ( 'could not create socket' );
        $connect = socket_connect ( $socket, '192.168.2.146', 6666 );
        socket_write ( $socket, '2-是否结束' ."\n");
        $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );

        socket_close($socket);

        return $str;

    }

js:

当视频上传之后,调用u_sockets向java函数发送上传视频路径和时间参数,之后通过ajax访问u_socketc方法,并设置每隔2s进行一次访问,用于查看当前java端函数的状态,如果得到的返回值$str为notend,说明有后台正在处理视频,此时显示请等待的div,并且上传按钮不可用,当返回end,后才可再次上传视频。

<script>
    $(document).ready(function reloadView(){
        $.post('{:url("u_socketc")}',{},function(data){
            $("#p").attr("style","display:none;");//隐藏div
            if(data=="notend"+"\n"){
                $("#p").attr("style","display:block;");//显示div
                $("#btn1").attr("disabled","disabled"); 
            }
            else if(data=="end"+"\n"){
                $("#p").attr("style","display:none;");//隐藏div
                $("#btn1").removeAttr("disabled");
            }
        });
    });
</script>
<script>
    function reloadView(){
        $.post('{:url("u_socketc")}',{},function(data){
            alert(data);
            $("#p").attr("style","display:none;");//隐藏div
            if(data=="notend"+"\n"){
                $("#p").attr("style","display:block;");//显示div
                $("#btn1").attr("disabled","disabled"); 
            }
            else if(data=="end"+"\n"){
                $("#p").attr("style","display:none;");//隐藏div
                $("#btn1").removeAttr("disabled");
            }
        });
    }
    setInterval('reloadView()',2000); 
</script>

html:

 <div class="row">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             必填信息
                        </div>
                        <div class="panel-body">
                            <div class="form-bottom">
                                <form role="form" action="{:url('uploadcourse')}" method="post" class="register-form"  id="myform" enctype="multipart/form-data">
<!--                                     <div class="form-group">
                                        <label>教师ID</label>
                                        <input  name="tid"  class="form-control" id="tid" >
                                    </div>
                                    <div class="form-group">
                                        <label>课程名称</label>
                                        <input  name="cname"  class="form-control" id="cname" >
                                    </div>
                                    <div class="form-group">
                                        <label>教室ID</label>
                                        <input  name="cid"  class="form-control" id="cid" >
                                    </div> -->
                                    <div class="form-group">
                                        <label class="control-label">开始时间</label>
                                        <div class="controls input-append date form_datetime" data-date="2018-06-16T05:25:07Z" data-date-format="yyyy-mm-dd HH:ii p" data-link-field="dtp_input1">
                                            <input size="16" type="text" value="" readonly style="height: 30px;" id="st" name="st">
                                            <span class="add-on" style="width:30px; height: 30px;"><i class="icon-remove"></i></span>
                                            <span class="add-on" style="width:30px; height: 30px;" id="s1"><i class="icon-th"></i></span>
                                        </div>
                                        <input type="hidden" id="dtp_input1" value="" /><br/>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label">结束时间</label>
                                        <div class="controls input-append date form_datetime" data-date="2018-06-16T05:25:07Z" data-date-format="yyyy-mm-dd HH:ii p" data-link-field="dtp_input1">
                                            <input size="16" type="text" value="" readonly style="height: 30px;" id="ft" name="ft">
                                            <span class="add-on" style="width:30px; height: 30px;"><i class="icon-remove"></i></span>
                                            <span class="add-on" style="width:30px; height: 30px;" id="s2"><i class="icon-th"></i></span>
                                        </div>
                                        <input type="hidden" id="dtp_input1" value="" /><br/>
                                    </div>
                                    <div class="form-group">
                                        <label >上传课程视频</label>
                                        <input type="file" name="file1" class="file" id="file1">
                                    </div>
                                    <p id="p" style="disabled:disabled;">视频处理中 ... ... <i class='fa fa-spinner'></i></p>
                                    <button type="submit" class="btn btn-primary" id="btn1">确认上传</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

效果图:


具体java端信息接收情况在队员电脑上没有截图,后期会补上。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页