使用codeigniter集成节点js和套接字IO

use*_*153 17 javascript php codeigniter node.js socket.io

如何在代码点火器中集成node.js和socket IO.

 <script>

        // create a new websocket
        var socket = io.connect('http://localhost:8000');
        // on message received we print all the data inside the #container div
        socket.on('notification', function (data) {
        var usersList = "<dl>";
        $.each(data.users,function(index,user){
            usersList += "<dt>" + user.user_name + "</dt>\n" +
                         "<dd>" + user.user_description + "\n" +
                            "<figure> <img class='img-polaroid' width='50px' src='" + user.user_img + "' /></figure>"
                         "</dd>";
        });
        usersList += "</dl>";
        $('#container').html(usersList);

        $('time').html('Last Update:' + data.time);
      });
    </script>
Run Code Online (Sandbox Code Playgroud)

在本提到的SO问题在这里.我的视图文件与codeigniter在,localhost/myproject但nodejs使用侦听端口localhost:8000.那我怎么连接socket IO.喜欢

 var socket = io.connect('http://localhost:8000');
 //here I need to make socket IO listen to localhost/myproject instead of localhost:8000 .
Run Code Online (Sandbox Code Playgroud)

这怎么可能?

sea*_*tes 4

我认为您误解了 socket.io 的工作原理。你永远不会听你的 CI 观点。您将始终在端口 8000 上向 NodeJS 服务器发送消息(并从其接收消息)。Codeigniter 的视图只是静态的,没有理由“监听”它,因为它只会加载一次。

您引用的答案的关键点是:

用户将使用 codeigniter URL,当打开页面时,我的 CI 视图页面上有这个脚本,它连接到我的 Nodejs 应用程序

因此,您可以使用 CI 视图加载浏览器,然后通过 CI 视图中的 JavaScript 侦听来自 NodeJS 服务器的事件。

然后,您还可以从 CI 视图中的 JavaScript 将事件推送到 NodeJS 服务器。