在Apache Web服务器上使用Node.js socket.io推送通知

问题描述 投票:1回答:3

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

基本假设是:1)使用Apache Web服务器作为主要应用程序Web服务器(由于我们所有的代码都在使用该服务器,因此必须使用)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尚未定义[中断此错误]'undefined'!= typeof io吗? io:module.exports

我真的需要帮助来了解它,并使它起作用。我也对替代解决方案持开放态度我真的需要帮助才能完成此任务。

apache node.js push-notification socket.io push
3个回答
2
投票

您想要实现的工作示例。第一个错误是客户端的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);

2
投票

[好吧,在IRC小组的巨大帮助下,我部分解决了,我创建了一个:1)端口80上的Apache上的HTML2)实时通知服务通过端口8080更新我的HTML]

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

p1.html(客户端)

<!doctype html><html>

<head>

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

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

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

$('#content').append(msg.message + '<br>')

});

</script>

</body>

</html>

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 && newtime != oldtime){

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

}

}

});

享受


0
投票

感谢#yanger您的代码帮助了我。

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

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

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

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

完全正常。我希望有人能阅读这篇文章并获得帮助。

© www.soinside.com 2019 - 2024. All rights reserved.