简体   繁体   English

与NodeJS通信

[英]Communication with NodeJS

Hey StackOverFlow Community, 嘿StackOverFlow社区,

I know I'm asking a very "simple" question, but I'm racking my brains through hours of googling. 我知道我在问一个非常“简单”的问题,但是我花了数小时的时间在搜寻中绞尽脑汁。

Can someone answer me how I can request from the client (Js) to my NodeJS server .eg if someone presses a button that the server does something (eg a function). 有人可以回答我如何从客户端(Js)向我的NodeJS服务器发出请求,例如,如果有人按下了该服务器执行某项操作(例如某个功能)的按钮。

Exactly the opposite is the case when a Function Serverside is executed, so that I can tell the client that it can get something displayed in the browser, for example 当执行功能服务器端时情况恰好相反,例如,我可以告诉客户端它可以在浏览器中显示某些内容。

I hope that someone can help me and send a good documentation! 我希望有人能帮助我并发送良好的文档! Or maybe another useful contribution! 或者,也许是另一个有用的贡献!

I thank you in advance 我提前谢谢你

With socket.io Client 使用socket.io客户端

<button type="button" onClick = "d()">Do</button>

function d(){
        socket.emit('do', function(data){
            socket.send("Do");
        });
}

Server 服务器

io.sockets.on('connection', function(socket){
socket.on('do', function(data){
        console.log('Do');
    });
}

This is just one of the million ways you can solve your problem. 这只是解决问题的数百万种方法之一。 I find this the simplest. 我发现这是最简单的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM