-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters