duanjia4220 2013-07-26 15:38
浏览 35
已采纳

使用node.js显示PHP应用程序中消息的实时通知[关闭]

I have a PHP application where people post different status messages. I want to implement realtime notifications in it.

That is if a user posts something, immediately other users accessing the site will get a prompt on their screen about a new post. I have heard you can use node.js to implement it. But I don't know how exactly it can be done. Any help will kindly be appreciated.

  • 写回答

2条回答 默认 最新

  • dpi96151 2013-07-26 15:42
    关注

    I suggest you to take a look at socket.io. Client send a message to node, node can save to db (or forward with a RESTful call to your php application) and notify all clients.

    It's not php but only nodejs and js. Enjoy ;)

    Example

    SERVER:

    var io = require('socket.io').listen(80);
    
    io.sockets.on('connection', function (socket) {
      socket.emit('news', { hello: 'world' });
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });
    

    CLIENT

    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    </script>
    

    You have to write what you want to be done in the client in on("yourEvent", function() { ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私