简体   繁体   English

Nodejs和PHP(客户端)

[英]Nodejs and PHP (Client side)

I have created the server side for nodejs and from my web application (PHP), I can send data to the nodejs as it is output from the commandline. 我已经为nodejs创建了服务器端,并从我的Web应用程序(PHP)中创建了数据,我可以将数据发送到nodejs,因为它是从命令行输出的。 Now I need to implement the client side on my web application to listen to the server and output this data. 现在,我需要在Web应用程序上实现客户端,以侦听服务器并输出此数据。

I have no idea how to do this the right way as I tried searching around for days and all I can find are articles on creating nodejs Server, no real world implementation example/tutorials. 我不知道如何以正确的方式执行此操作,因为我尝试了几天的搜索,而我所能找到的只是有关创建nodejs Server的文章,没有实际的实现示例/教程。 The nearest eexample I can find is http://goo.gl/zi2xqX where its using a setimeout function like so: 我能找到的最接近的eexample是http://goo.gl/zi2xqX ,其中它使用setimeout函数,如下所示:

function load_tweets() {  
        $.getJSON("/stream", function(tweets) {  
            $.each(tweets, function() {  
                $("<li>").html(this.text).prependTo(tweet_list);  
            });  
            load_tweets();  
        });  
    }  

    setTimeout(load_tweets, 1000);  

Is this the correct way to retrieve data from nodejs server? 这是从Node.js服务器检索数据的正确方法吗?

If you are using socket.io use something like this: 如果您使用的是socket.io,请使用以下代码:

Server 服务器

io.sockets.socket(clientId).volatile.emit("requestName", data);

Client 客户

socket.on("requestName", function (data) {
    //do stuff with data
});

This is assuming you have clients connecting and you are recording there connection Ids. 这是假设您有客户端连接,并且正在那里记录连接ID。 If you don't, I recommend looking up some socket.io tutorials as there are much better ones out there than I can write. 如果不这样做,我建议您查阅一些socket.io教程,因为那里有比我写的更好的教程。

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

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