【问题标题】:Push notifications using Node.js socket.io on apache web-server在 apache 网络服务器上使用 Node.js socket.io 推送通知
【发布时间】:2013-01-10 10:58:48
【问题描述】:

我正在尝试了解所有推送通知的工作原理。我试图对推送技术进行一些测试,但到目前为止我失败了。

基本假设是:
1) 使用 Apache web-server 作为主应用程序 web-server(必须的,因为我们所有的代码都在使用它)
2)node.js技术中的跨浏览器推送通知服务器(提供socket.io,因为它是跨浏览器)。

到目前为止我失败了,这是我的代码(p1.html):

<!doctype html>
<html>
<head>

<meta charset="UTF-8">
<title>P1</title>
</head>
<body>

<h1>P1</h1>
<section id="content"></section>
<script src="/socket.io.js"></script> <!--socket.io-->
<script src="/socket.js"></script> <!--socket.io-client-->
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>

var socket = io.connect('http://localhost:8080');

socket.on('notification', function (data) {
$('#content').append(data.message + '<br>')

});

</script>

</body>

</html>

和我的服务器脚本(p1.js):

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, url = require('url')
app.listen(8080);

console.log("creating a connection");
io.sockets.on( 'connection', function ( socket ) {

console.log("runing time");
sendTimeMessage(socket);
});

function sendTimeMessage(socket){

console.log("in time");
var time= new Date().getTime();
console.log(time);
socket.volatile.emit( 'notification' , time );
setTimeout(sendTimeMessage, 5000);
}

function handler (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("");
}

function sendMessage(message) {

io.sockets.emit('notification', {'message': message});
}

  • 我将示例中的 IP 更改为本地主机,所以我希望语法没有错误。
  • 当我运行时,Apache Web 服务器是显示数据的服务器,其想法是让 socket-io 更新几个字段。

当前状态:
1. 如果我不添加 socket.io-client js 文件,我会收到 socket.io-client 的引用错误
2. 如果我添加 socket.io-client 我得到“ReferenceError: require is not defined [中断此错误] 'undefined' != typeof io ? io : 模块.exports

我真的需要帮助来理解它并让它发挥作用。我也对替代解决方案持开放态度 我真的需要帮助才能完成这项工作。

【问题讨论】:

  • 好吧,我看到你在 require('url') 的末尾没有分号。

标签: apache node.js push-notification socket.io push


【解决方案1】:

您想要实现的工作示例。第一个错误是客户端的javascript路径错误,正确的是/socket.io/socket.io.js。第二个错误是使用了不存在的socket.volatile。

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, url = require('url')


console.log("creating a connection");

io.sockets.on( 'connection', function ( socket ) {
  console.log("runing time");
  sendTimeMessage(socket);
});

function sendTimeMessage(socket){
  console.log("in time");
  var now= new Date().getTime();
  socket.emit('notification', {'message': now});
  setTimeout(function() {
    socket.emit('notification', {'message': "after 5s"});
  },5000);
}

function handler (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end("<html><script src=\"/socket.io/socket.io.js\"></script> <!--socket.io--><script>io.connect().on('notification', function (data) {console.log(data)});</script></html>");
}

app.listen(8080);

【讨论】:

  • 谢谢,但是...您正在使用 socket.io 作为网络服务器,这不是我想要的。我有一个运行某些代码的 Apache 网络服务器。我想为我的网络应用程序添加一项功能。我不想替换(并重新调试)我的整个代码。我想添加一个 socket.io 通知服务器,它在某些页面上接收新连接,并且能够推送数据和更新代码。如果你看我的例子,我有一个 Apache 显示的页面,我尝试将 socket.io 运行到 Apache 旁边。这个“服务”将为我做推送通知
【解决方案2】:

好的,在 IRC 上的人的大力帮助下,我部分解决了这个问题,我创建了一个: 1) Apache 上的 80 端口上的 HTML 2) 实时通知服务通过端口 8080 更新我的 HTML

(从函数到达的值可能仍然存在代码问题,导致它没有完全调试)

p1.html(客户端)

&lt;!doctype html&gt;
&lt;html&gt;

&lt;head&gt;

&lt;meta charset="UTF-8"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;section id="content"&gt;&lt;/section&gt;
&lt;script src="/node_modules/socket.io-client/dist/socket.io.js"&gt;&lt;/script&gt;
&lt;script src="http://code.jquery.com/jquery-1.7.1.min.js"&gt;&lt;/script&gt;
&lt;script&gt;

var socket = io.connect('http://10.10.10.1:8080');

socket.on('notification', function (from,msg) {

$('#content').append(msg.message + '&lt;br&gt;')

});

&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

p1.js(服务)

var io = require('socket.io').listen(8080)
console.log("creating a connection");
io.sockets.on( 'connection', function ( socket ) {

console.log("runing time");
var oldtime= new Date().getTime();
while (1){

var newtime= new Date().getTime();
if (newtime%5323==0 &amp;&amp; newtime != oldtime){

oldtime = newtime;
console.log(newtime);
socket.emit( 'notification' , {'message': "the time is - " + newtime} );

}

}

});

享受

【讨论】:

    【解决方案3】:

    谢谢#yanger 你的代码帮助了我。

    我想添加评论。 但我还不能使用评论。

    就我而言,我想发出实时警报。 我使用80端口的Web服务器和81端口的报警服务器。

    所以我只使用这段代码。 (Client.js)

    var socket = io.connect(':81');
    

    它完全有效。 我希望有人会阅读这篇文章并获得帮助。

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      相关资源
      最近更新 更多