Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unity] Bug: 运行puerts的nodejs http示例,localhost访问不到 #1745

Closed
3 tasks done
1162064779 opened this issue May 31, 2024 · 6 comments
Closed
3 tasks done
Assignees
Labels
bug Something isn't working Unity

Comments

@1162064779
Copy link

前置阅读 | Pre-reading

Puer的版本 | Puer Version

2.0.5

Unity的版本 | Unity Version

2022.3.23

发生在哪个平台 | Platform

Editor(win)

错误信息 | Error Message

运行puerts的nodejs http示例,例如mjs:
const server = require('node:http').createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('

Hello, World!

This is a simple Node.js HTTP server using ES modules.

');
});
const PORT = 9223;
server.listen(PORT, () => {
console.log(Server is running at http://localhost:${PORT});
});
或者是直接通过env.Eval调用:
const http = require('node:http');
http.createServer((req, res)=> { res.writeHead(200);res.end('helloworld') }).listen(9223);
在editor下运行,不会报错,netstat显示服务端口正在运行,但http get没有反应
另外,当尝试使用import http时,报错module not found,为什么require就能找到呢?用的应该是同一个node.dll吧

问题重现 | Bug reproduce

稳定复现,get不了服务的问题

@1162064779 1162064779 added bug Something isn't working Unity labels May 31, 2024
@chexiongsheng
Copy link
Collaborator

chexiongsheng commented May 31, 2024

const PORT = 8081

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});

    response.end('Hello World\n');
}).listen(PORT);

console.log(`Server running at http://127.0.0.1:${PORT}/`);

@1162064779
Copy link
Author

const PORT = 8081

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});

    response.end('Hello World\n');
}).listen(PORT);

console.log(`Server running at http://127.0.0.1:${PORT}/`);

您好,感谢回复,我想知道这段代码和我原本的有什么本质区别嘛?我现在运行下来还是一样的问题,创建的server还是无法访问:
image

@chexiongsheng
Copy link
Collaborator

如果是unity,你应该要在update那调用JsEnv.Tick

@1162064779
Copy link
Author

如果是unity,你应该要在update那调用JsEnv.Tick

十分感谢!这对我很有用!我会去仔细研究这个函数的含义,感谢您的解答!

@1162064779 1162064779 reopened this Jun 3, 2024
@1162064779
Copy link
Author

@chexiongsheng 我很遗憾的是,虽然nodejs在editor中好使了,但在我打包出的unity apk运行在手机上时,他依旧无法成功创建服务,我很确定我下载的puerts是最新的且backend为nodejs:
image
因为editor下好使但移动端不好使,所以我只能猜测是我的node相关lib有问题,我发现下载下来的lib中很多这样的meta文件
image
这和我在backend-nodejs项目中看到的类似,我猜测可能是这些.a都被一同打包进了libpuerts.so,所以不再需要了?我简单的使用您backend-nodejs项目中的armv8产物替换这里的lib发现编译不过,我很想知道这里面是哪一块出了问题?

@chexiongsheng
Copy link
Collaborator

editor可以,手机上应该就没问题。你看是不是场景没启动,脚本没启动,防火墙之类的问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Unity
Projects
None yet
Development

No branches or pull requests

2 participants