美文网首页
wrs-httpserver本地http service插件

wrs-httpserver本地http service插件

作者: 浪人残风 | 来源:发表于2021-06-30 14:40 被阅读0次

    前言

    wrs-httpserver是Android、iOS搭建APP本地web service的插件,常用的场景有: <br />

    1. app本地离线播放m3u8视频
    2. app本地浏览web网站,支持绝对路径,_www ,_doc,_documents,_downloads等相对路径需要转成本地绝对路径,下面有转换方法。

    功能

    • 启动http service,可配置多个web站点服务

    wrs-httpserver插件

    场景 1. 直接访问在uniapp项目源码里的web站点

    var httpServer = uni.requireNativePlugin("wrs-httpserver");
    // 把_www相对路径转为绝对路径
    var absPath = plus.io.convertLocalFileSystemURL('_www');
    // Android获取的absPath以/结尾,iOS获取的absPath不是/结尾
    if (absPath.endWith('/')) {
        absPath = absPath.substring(0, absPath.length - 1);
    }
    var params = {
        port: 8030,  // http service的端口
        staticWebs: [
          {
            urlPath: '/register', // 站点请求的url
            directoryPath: absPath + '/static/registerSys'  // web站点文件绝对路径,这个路径是绝对路径
          },
         {
            urlPath: '/login',
            directoryPath: absPath + '/static/loginSys'
          }
        ]
    };
    httpServer.startServer(params, (resp) => {
        if (resp.code == 0) {
            console.log("httpServer启动成功:" + resp.serverHost);
        } else {
            console.log("httpServer启动失败:" + resp.msg);
        }
    });
    

    启动成功后,在浏览器输入resp.serverHost + port + urlPath + 网站文件,如:

    http://192.168.6.31:8030/register/index.html
    http://192.168.6.31:8030/login/index.html
    

    loginSys、loginSys这两个站点的源码放到HBuildx项目里的static文件夹里


    image.png

    场景 2. 通过接口网络下载web站点源码并访问

                                   // 下载web站点压缩文件
                    uni.downloadFile({
                        url: "http://192.168.6.89:8080/video/registerSys.zip", //仅为示例,并非真实的资源
                        success: (res) => {
                            if (res.statusCode === 200) {
                                var zipfile = res.tempFilePath;
                                                            // 下载后的压缩文件解压到_downloads目录
                                var targetPath = '_downloads/';
                                var absPath = plus.io.convertLocalFileSystemURL(targetPath);
                                // 经测试,修正在华为Android 10的系统上解压的解压目录,Android上使用绝对路径
                                switch (uni.getSystemInfoSync().platform) {
                                    case 'android':
                                        targetPath = plus.io.convertLocalFileSystemURL(targetPath);
                                        break;
                                    case 'ios':
                                        targetPath = '_downloads/';
                                        break;
                                    default:
    
                                        break;
                                }
                                plus.zip.decompress(zipfile, targetPath,
                                    function() {
                                        var path = plus.io.convertLocalFileSystemURL(targetPath);
                                        console.log('解压成功绝对路径:' + path);
                                    },
                                    function(error) {
                                        console.log('解压解压失败:' + error);
                                    });
                            } else {
                                console.log('下载失败');
                            }
                        }
                    });
    

    解压后_downloads目录已经有站点文件了,在启动http server时把_downloads下的站点也加进入即可:

                                    // _www ,_doc,_documents,_downloads
                    var staticWebs = [];
                    var paths = ['_www', '_downloads'];
                    for (var i = 0; i < paths.length; i++) {
                        var path = paths[i];
                        var absPath = plus.io.convertLocalFileSystemURL(path);
                        // Android获取的absPath以/结尾,iOS获取的absPath不是/结尾
                        if (absPath.endWith('/')) {
                            absPath = absPath.substring(0, absPath.length - 1);
                        }
                        switch (i) {
                            case 0: {
                                staticWebs.push({
                                    urlPath: '/staticLoginSys',
                                    directoryPath: absPath + '/static/loginSys',
                                });
                                staticWebs.push({
                                    urlPath: '/',
                                    directoryPath: absPath + '/static/registerSys',
                                });
                            }
                            break;
                        case 1: {
                            staticWebs.push({
                                urlPath: '/downloadsSys',
                                directoryPath: absPath + '/registerSys',
                            });
                        }
                        break;
                        default:
                            break;
                        }
                        console.log('path:' + path + '  绝对路径:' + absPath);
                    }
                    var appid = plus.runtime.appid;
                    var params = {
                        port: 8030,
                        staticWebs: staticWebs
                    };
                    this.staticWebs = staticWebs;
                    httpServer.startServer(params, (resp) => {
                        if (resp.code == 0) {
                            console.log("httpServer启动成功:" + resp.serverHost);
                        } else {
                            console.log("httpServer启动失败:" + resp.msg);
                        }
                    });
    

    _downloads下web站点的访问地址是:

    http://192.168.6.31:8030/downloadsSys/index.html
    

    如果Android打包时出现如下错误,请再次提交打包,这是uniapp云打包下载库没有及时下载到造成的,需要多试几次


    image.png

    如果觉得可以就点个👍吧,欢迎粉丝收藏,土豪打赏,您的关注就是我们创作的动力!

    相关文章

      网友评论

          本文标题:wrs-httpserver本地http service插件

          本文链接:https://www.haomeiwen.com/subject/oijrultx.html