Skip to content

Commit

Permalink
t
Browse files Browse the repository at this point in the history
  • Loading branch information
helloyork committed Jan 1, 2024
1 parent e51101e commit 7312b03
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/模板代码/简易玩家数据通信/clientIndex.js
Original file line number Diff line number Diff line change
@@ -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)


45 changes: 45 additions & 0 deletions src/模板代码/简易玩家数据通信/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
4 changes: 2 additions & 2 deletions src/端连接/RemoteWrapper/1.1.0/Remote.client.js
Original file line number Diff line number Diff line change
@@ -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 }
}
Expand Down
2 changes: 2 additions & 0 deletions src/端连接/RemoteWrapper/1.1.0/Remote.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class RemoteGroups {
/**
* @callback WhenGetMessage
* @param {JSONValue} arg 客户端发送的数据
* @param {GameEntity} entity 玩家实体
*/
/**
* @typedef {Object} RemoteServerEventToken
Expand Down Expand Up @@ -264,3 +265,4 @@ class RemoteServerWrapper {
return new Error(`[Remote Server] ${this.constructor.quitCode[c]}`);
}
}
module.exports = RemoteServerWrapper

0 comments on commit 7312b03

Please sign in to comment.