如何连接nodejs和java?
我需要我的节点应用程序能够从已运行的Java应用程序或从Node启动Java应用程序发送和收到数据。
那么,有没有办法将数据从nodejs发送到Java并返回?
我的目标是拥有这样的东西:
// In Java:
J2JSInterface.setReciever(msg -> System.out.println("Message from JS: " + msg));
J2JSInterface.send("Hello from Java!");
// In Java console:
// Message from JS: Hello from JS!
// In Node:
JS2JInterface.setReciever(msg => console.log(`Message from Java: ${msg}`))
JS2JInterface.send("Hello from JS!")
// In Node console:
// Message from Java: Hello from Java!
PS:我尝试在一个C ++库中使用JNI和Node API在一起,(代码),但它行不通,因为节点和Java将加载库的两个独立实例,并且这些实例无法彼此相互作用。 (还是可以?)
I need for my Node application to be able to send and recieve data to/from an already running Java app or to launch Java app from Node.
So, is there a way to send data from NodeJS to Java and back?
My goal is to have something like this:
// In Java:
J2JSInterface.setReciever(msg -> System.out.println("Message from JS: " + msg));
J2JSInterface.send("Hello from Java!");
// In Java console:
// Message from JS: Hello from JS!
// In Node:
JS2JInterface.setReciever(msg => console.log(`Message from Java: ${msg}`))
JS2JInterface.send("Hello from JS!")
// In Node console:
// Message from Java: Hello from Java!
P.S.: I Tried using JNI and Node Api in one C++ library together, (code) but it won't work because Node and Java will load two independent instances of the library and these instances can't interact with each other. (or can they?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的倾向于过度复杂化的事情...
TCP是解决真正快速交流的解决方案。当我检查延迟时,我感到震惊,因为它大约是0到1毫秒。感谢ahmetuğur指出我可以使用WebSockets!
I really tend to overcomplicate things...
TCP is the solution for really fast communicating. When I checked the latency, I was shocked, because it was around 0 to 1 ms. Thanks to AhmetUğur for pointing out that I could just use websockets!