diff --git "a/src/\346\250\241\346\235\277\344\273\243\347\240\201/\347\256\200\346\230\223\347\216\251\345\256\266\346\225\260\346\215\256\351\200\232\344\277\241/clientIndex.js" "b/src/\346\250\241\346\235\277\344\273\243\347\240\201/\347\256\200\346\230\223\347\216\251\345\256\266\346\225\260\346\215\256\351\200\232\344\277\241/clientIndex.js" new file mode 100644 index 0000000..26c7770 --- /dev/null +++ "b/src/\346\250\241\346\235\277\344\273\243\347\240\201/\347\256\200\346\230\223\347\216\251\345\256\266\346\225\260\346\215\256\351\200\232\344\277\241/clientIndex.js" @@ -0,0 +1,36 @@ + +// 导入RCW,如果已有则不需要这一步 +const RemoteClientWrapper = require("../../端连接/RemoteWrapper/1.1.0/Remote.client.js"); +// 初始化RCW +const rcw = RemoteClientWrapper(80); + +// 导入UiNodeWrapper,如果已有则不需要这一步 +const UiNodeWrapper = require("UiWrapper.js").createUiNodeWrapper(); +// 配置uiWrapper +const uiWrapper = new UiNodeWrapper(ui); + +// 定义与一个fetch函数 +function fetch(path, content) { + return new Promise(resolve => { + rcw.communicate({ path, ...content }, (v) => { + resolve(v); + }) + }) +} + +// 由于服务端特性,我们需要先等待 +setTimeout(() => { + fetch("/user/profile", { + method: "GET", + }).then(v => { + // 这里是设置数据在GUI上的显示 + uiWrapper.findChildByName("coin") + .config("textContent", v.coin); + uiWrapper.findChildByName("photo") + .config("image", v.photo); + uiWrapper.findChildByName("name") + .config("textContent", v.name); + }) +}, 1000) + + diff --git "a/src/\346\250\241\346\235\277\344\273\243\347\240\201/\347\256\200\346\230\223\347\216\251\345\256\266\346\225\260\346\215\256\351\200\232\344\277\241/index.js" "b/src/\346\250\241\346\235\277\344\273\243\347\240\201/\347\256\200\346\230\223\347\216\251\345\256\266\346\225\260\346\215\256\351\200\232\344\277\241/index.js" new file mode 100644 index 0000000..fb24f32 --- /dev/null +++ "b/src/\346\250\241\346\235\277\344\273\243\347\240\201/\347\256\200\346\230\223\347\216\251\345\256\266\346\225\260\346\215\256\351\200\232\344\277\241/index.js" @@ -0,0 +1,45 @@ +// 导入RSW,如果已有则不需要这一步 +const RemoteServerWrapper = require("../../端连接/RemoteWrapper/1.1.0/Remote.server.js"); + +class WebApp { + /** + * @param {RemoteServerWrapper} rms + */ + constructor(rms) { + this.rms = rms; + } + get(path, f) { + this.rms.onMessage("communicate", async (request, entity) => { + if (request.path === path && request.method === "GET") { + return await f(request, entity); + } + }) + } + post(path, f) { + this.rms.onMessage("communicate", async (request, entity) => { + if (request.path === path && request.method === "POST") { + return await f(request, entity); + } + }) + } +} + +// 开始监听端口80 +let rms = new RemoteServerWrapper([], { + port: 80 +}); + +// 创建一个模拟Web应用 +let app = new WebApp(rms); +// 监听/user/profile上的GET操作 +app.get("/user/profile", async (request, entity) => { + return await GET_USER_PROFILE(entity); // 返回了Promise {coin:10, photo:"uri", name:"Nomen"} +}); + +// 玩家状态变化时监听或取消监听 +world.onPlayerJoin(({ entity }) => { + rms.add(entity); +}); +world.onPlayerLeave(({ entity }) => { + rms.cancel(entity); +}); diff --git "a/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.client.js" "b/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.client.js" index b20df1f..d20ea5b 100644 --- "a/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.client.js" +++ "b/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.client.js" @@ -1,6 +1,6 @@ -function RemoteClientWrapper() { - let events = [], seed = 2024, dport = 6000,random = () => (seed = (seed * 9301 + 49297) % 233280, parseInt(seed / 233280.0 * 1000000)); +function RemoteClientWrapper(port) { + let events = [], seed = 2024, dport = port || 6000,random = () => (seed = (seed * 9301 + 49297) % 233280, parseInt(seed / 233280.0 * 1000000)); function wrap(type, data, id, port = dport) { return { type, data, id, port } } diff --git "a/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.server.js" "b/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.server.js" index 6331a4b..7cb8768 100644 --- "a/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.server.js" +++ "b/src/\347\253\257\350\277\236\346\216\245/RemoteWrapper/1.1.0/Remote.server.js" @@ -38,6 +38,7 @@ class RemoteGroups { /** * @callback WhenGetMessage * @param {JSONValue} arg 客户端发送的数据 + * @param {GameEntity} entity 玩家实体 */ /** * @typedef {Object} RemoteServerEventToken @@ -264,3 +265,4 @@ class RemoteServerWrapper { return new Error(`[Remote Server] ${this.constructor.quitCode[c]}`); } } +module.exports = RemoteServerWrapper \ No newline at end of file