【问题标题】:What happens if node js doesn't find a client html file?如果 node js 找不到客户端 html 文件会怎样?
【发布时间】:2015-07-23 16:20:24
【问题描述】:

我正在尝试为我的 pHP 应用程序实现推送服务器。 我最初的想法是,只要我在我的客户端上调用事件,比如说 message_view.php 文件。我不会有发出节点 js 事件的问题。

但我看到大部分在线教程都使用了类似我不理解的内容?

    fs.readFile(__dirname + '/client.html') ...//html files
    or 
    response.sendfile('hello world') 

在他们启动服务器之后。然后他们在我的messages_view.php文件中应该在客户端html文件中添加如下事件和逻辑。

<script src="socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

和这样的套接字发射事件:

<script>

// create a new websocket
var socket = io.connect('http://localhost:8000/?profile_id=abcde2');
//..I want to code here to update my  divs.
</script>

我需要什么才能在我的 PHP 文件上发出此消息。非常感谢!

【问题讨论】:

  • 你是否在节点中创建服务器?
  • 是的,先生。我已经创建了服务器,我唯一想要的就是从 y PHp 文件中发出这些 ebents
  • 我似乎有点不清楚,但是,如果您问是否需要提供 .html 才能使用 websockets,答案是否定的。
  • 为什么不清楚?我要求提供 php 文件而不用发送文件发送它们?
  • 和...那是发送文件的 node.js 代码?除非套接字服务器还需要提供 html,否则套接字服务器不需要它。看来您不希望那样,而是想用 php 提供 html。

标签: javascript php node.js socket.io


【解决方案1】:

如果你想用 php 提供 html,并且有一个 node.js 套接字服务器,那么套接字服务器不需要提供 html,所以你可以省略你正在使用的大部分示例。只需将脚本包含在为 html 页面提供服务的 .php 中,并确保脚本的 url 正确地定位到套接字服务器。

【讨论】:

  • 谢谢!它是实际的服务器代码还是 socket.io 文件? ex.server.js
  • 示例包含在.html文件中的socket.io js文件。
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 2020-09-27
  • 2020-04-10
  • 2021-03-12
  • 2022-01-26
  • 2011-07-28
  • 2016-07-29
  • 1970-01-01
相关资源
最近更新 更多