npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egret-socket-pomelo-client-adaptor

v1.0.2

Published

egret WebSocket adaptor of pomelo-client

Downloads

10

Readme

egret-socket-pomelo-client-adaptor

H5 游戏的前端已经使用了 egret.WebSocket 做为通讯库进行开发后,需要接入或适配后端 Pomelo 通信协议时可以使用此库。

使用此适配器,只需要简单地更改 Socket 实例的创建方式,无需大幅度重构即可快速适配至使用 Pomelo 客户端与 Pomelo 后端通信。

Usage

使用 egret.WebSocket 的原代码:

const sock = new egret.WebSocket();
sock.connect(host, port);

现需接入使用 Pomelo 的后端,可使用适配器进行适配

const sock = new Adaptor(new egret.WebSocket());

原理

下面 egret.WebSocket 原型上的属性和方法

Object.keys(egret.WebSocket.prototype);
// (12) ["constructor", "connect", "close", "onConnect", "onClose", "onError", "onSocketData", "flush", "writeUTF", "readUTF", "connected", "__class__"]

在适配器的原型上同样实现了其中的部分属性和方法(部分方法的参数有所变化,请阅读 200 行不到的源码以知悉),同时因为创建适配器时传入了 egret.WebSocket 实例,我们可以在适配器的方法里为该实例添加事件监听、改变这个实例的属性和状态、调用其方法、触发其事件。

由于不同项目的消息格式不尽相同,你可以通过覆写适配器原型上的两个方法进行适配器的定制:

  1. Adaptor.prototype.config() - 定制自己的消息格式或进行其他任何你需要的功能增强
  2. Adaptor.prototype.msgCook(msg) - adaptor.writeUTF() 调用时对消息进行个性化处理
  3. Adaptor.prototype._buildMsg() - 对后端 Pomelo 返回的消息进行编辑构造
// 为实例增加一些属性
Adaptor.prototype.config = function (nameKey, dataKey) {
    this.nameKey = nameKey;
    this.dataKey = dataKey;
};

// 每次 writeUTF 时都会调用此 msgCook
Adaptor.prototype.msgCook = function (msg) {
    msg['__route__'] = 'ttl.gameHandler.msgHandler';
};

// 收到任何服务器返回(包括 Response 和 Push)时都调用此方法来编辑重写返回内容
Adaptor._buildMsg = function (route, body) {
    var msg = {};
    msg[this.nameKey] = name || '_';
    msg[this.dataKey] = body;
    return msg;
};

限制

  1. 不支持使用二进制格式的 egret.WebSocket 实例