jquery trim重写

jquery trim重写

String.prototype.trim = function(char, type) {
    if (char) {
        if (type == 'left') {
            return this.replace(new RegExp('^\\' + char + '+', 'g'), '');
        } else if (type == 'right') {
            return this.replace(new RegExp('\\' + char + '+$', 'g'), '');
        }
        return this.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
    }
    return this.replace(/^\s+|\s+$/g, '');
};

 str = '/Ruchee/';
    console.log(str.trim('/', 'left')); // Ruchee/

linux shell每秒执行的方法

linux shell每秒执行的方法

startphpfpm.sh文件如下
#!/bin/sh
while [ true ]; do
sleep 1
currTime=$(date +"%Y-%m-%d %T");
echo "ddd  ${currTime}" >> /data/web/mircoweb/wwwroot/App/Home/Controller/reloadinfo.log;
done

然后运行

 nohup sh startphpfpm.sh &

后台运行

如果禁止 直接 ps | grep startphpfpm.sh 找到进程kill -9 进程树就行

swoole是支持wss的方法

swoole是支持wss的方法

1 先让swoole支持ssl

首先编译的时候 加上–enable-openssl

然后打命令查看/usr/local/php/bin/php –ri swoole

swoole

swoole support => enabled
Version => 1.9.23
Author => tianfeng.han[email: mikan.tenny@gmail.com]
epoll => enabled
eventfd => enabled
timerfd => enabled
signalfd => enabled
cpu affinity => enabled
spinlock => enabled
rwlock => enabled
async http/websocket client => enabled


openssl => enabled


Linux Native AIO => enabled
pcre => enabled
zlib => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled

Directive => Local Value => Master Value
swoole.aio_thread_num => 2 => 2
swoole.display_errors => On => On
swoole.use_namespace => Off => Off
swoole.fast_serialize => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

看到openssl => enabled 就代表可以了

然后swoole写如下代码就行

$ws = new swoole_websocket_server("0.0.0.0", 9908, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$ws->set(array(
    'worker_num' => 1,
    'max_conn'   => 65535,
     'ssl_cert_file'=>'cert/1965653_wxserver.knowway.cn.pem',
    'ssl_key_file' =>'/cert/1965653_wxserver.knowway.cn.key',
    // 'daemonize'       => true,
    'backlog'    => 128,
    // 'task_worker_num' => 1,
));

ps:前端必须要用wss才能连接 ws会链接失败

egret微信好友排行榜制作教程

egret微信好友排行榜制作教程

1 先设置开放域目录
打开game.json文件
{
“deviceOrientation”: “portrait”,
“openDataContext”: “src/myOpenDataContext”
}
2 主域中 存入玩家信息
wx.setUserCloudStorage({
KVDataList: [{ key: ‘score’, value: “33” }],
success: res => {
console.log(res);

},
fail: res => {
console.log(res);
}
});

3 开放域中 读取数据


wx.getFriendCloudStorage({
  keyList: ['score'], // 你要获取的、托管在微信后台都key
  success: res => {
    console.log(res.data);
  }
});

4 下面最难的就是绘制排行榜了
首先开放域中调用默认的sharecanvas画布 给他填充个红色长方形

let sharedCanvas = wx.getSharedCanvas()
let context = sharedCanvas.getContext('2d')
context.fillStyle = 'red'
context.fillRect(0, 0, 300, 600)

然后在主域上显示

 let openDataContext = wx.getOpenDataContext()
    let sharedCanvas1 = openDataContext.canvas

    let canvas1 = wx.createCanvas()
    let context = canvas1.getContext('2d')
    context.drawImage(sharedCanvas1, 10, 10)

egret 好友排行榜demo https://github.com/egret-labs/egret-target-wxgame/tree/master/demos/openDataContext

egret加载网络图片的方法

egret加载网络图片的方法

protected onComplete() {

		var imgLoader: egret.ImageLoader = new egret.ImageLoader;
		egret.ImageLoader.crossOrigin = "anonymous";
		imgLoader.load("https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJCWpvVtU7gUyvovhiaS96cc4nLlHwWfX8CllhABoiaGvrXexXppgofV5ZvtvicBrcJr4yJkKpObibesA/132");
		imgLoader.once(egret.Event.COMPLETE, this.imgLoadHandler, this);
	}
	private imgLoadHandler(evt: egret.Event): void {
		if (evt.currentTarget.data) {
			egret.log("加载头像成功: " + evt.currentTarget.data);
			let texture = new egret.Texture();
			texture.bitmapData = evt.currentTarget.data;
			let bitmap = new egret.Bitmap(texture);
			bitmap.x = 200;
			bitmap.y = 200;
			this.addChild(bitmap);
		}
	}

egret 摇杆代码

egret 摇杆代码

        //  实现原理就是 注意 定时器更新的坐标速度一定要保证一致所以我用了相等距离计算的
        //要拖拽的对象
        var draggedObject: egret.Shape;
        var offsetX: number;
        var offsetY: number;
        var chushiX;
        var chushiY;
        var jiaoduX;
        var jiaoduY;
        var pianyijiaoduX;
        var pianyijiaoduY;

        //画一个白色的正方形
        let stageW = this.stage.stageWidth;

        let stageH = this.stage.stageHeight;
        var bg1: egret.Shape = new egret.Shape();
        bg1.graphics.beginFill(0xffffff);
        bg1.graphics.drawRect(0, 0, stageW, stageH);
        bg1.graphics.endFill();
        bg1.x = 0;
        bg1.y = 0;
        bg1.touchEnabled = true;
        this.addChild(bg1);

        //画一个红色的圆
        var circle: egret.Shape = new egret.Shape();
        circle.graphics.beginFill(0xff0000);
        circle.graphics.drawCircle(0, 0, 15);
        circle.graphics.endFill();
        circle.x = 163;
        circle.y = 466;
        this.addChild(circle);


        //黑色半透明圆 园的achor就是中心
        var circle1: egret.Shape = new egret.Shape();
        circle1.graphics.beginFill(0x000000);
        circle1.graphics.drawCircle(0, 0, 65);
        circle1.x = circle.x - 10;
        circle1.y = circle.y - 10;
        circle1.graphics.endFill();
        circle1.alpha = 0.2;
        this.addChild(circle1);

        //画一个蓝色的正方形
        var square: egret.Shape = new egret.Shape();
        square.graphics.beginFill(0x0000ff);
        square.graphics.drawRect(0, 0, 100, 100);
        square.graphics.endFill();
        square.x = 33;
        square.y = 66;
        this.addChild(square);
        var kaishimove = 0;

        //增加圆形的触摸监听
        circle.touchEnabled = true;




        this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, startMove, this);


        function startMove(e: egret.TouchEvent): void {
            chushiX = e.stageX;
            chushiY = e.stageY;
            circle.x = e.stageX;
            circle.y = e.stageY;
            circle1.x = circle.x - 10;
            circle1.y = circle.y - 10;

            //把触摸的对象放在显示列表的顶层
            this.addChild(circle);
            //手指在屏幕上移动,会触发 onMove 方法
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);



        }

        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, stopMove, this);

        this.addEventListener(egret.Event.ENTER_FRAME, onEnterFrame, this);

        function onEnterFrame(e: egret.Event) {

            if (kaishimove == 1) {
                console.log(111);
                square.x += pianyijiaoduX;
                square.y += pianyijiaoduY;
            }


        }

        function stopMove(e: egret.TouchEvent) {
            kaishimove = 0;
            console.log(22);
            //手指离开屏幕,移除手指移动的监听
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);
        }
        function onMove(e: egret.TouchEvent): void {
            kaishimove = 1;

            circle.x = e.stageX;
            circle.y = e.stageY;
            jiaoduX = e.stageX - chushiX;
            jiaoduY = e.stageY - chushiY;

            var point2: egret.Point = new egret.Point(jiaoduX, jiaoduY);
            point2.normalize(1);
            pianyijiaoduX = point2.x;
            pianyijiaoduY = point2.y;


            console.log('pianyijiaoduX=' + pianyijiaoduX);

            console.log('pianyijiaoduY=' + pianyijiaoduY);
        }

ps: 触摸事件不会向下传递 所以我统一给舞台加了个事件