Angularjs app的多个实例

时间:2016-04-29 10:04:37

标签: javascript angularjs

我很想知道是否可以在两个单独的浏览器上运行相同的应用程序并从一个浏览器获取更新?

让我们说我在Chrome上运行应用:http://127.0.0.1:8000/# 并且在Firefox或其他隐身Chrome选项卡中打开,当任何打开的会话更新时,我想在其他会话上获得更新/警报。 任何方向都会受到赞赏。

由于

1 个答案:

答案 0 :(得分:0)

我为此创建了2个网络服务器:

一个运行web-socket。 其他应用程序。

websocket example nodejs:

#!/usr/bin/env node

var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var port = process.env.PORT || 5000;

var app = express();
    app.use(express.static(__dirname+ "/../"));
    app.get('/test', function(req, res, next) {
       console.log('receiving get request');
    });
    app.post('/test', function(req, res, next) {
       console.log('receiving post request');
    });
    app.listen(80);

    //console.log("app listening on %d ", 80);

var server = http.createServer(app);
    server.listen(port);

console.log("http server listening on %d", port);


var userId;
var wss = new WebSocketServer({server: server});
    wss.on("connection", function (ws) {

    console.info("websocket connection open");

    var timestamp = new Date().getTime();
    userId = timestamp;

    ws.send(JSON.stringify({msgType:"onOpenConnection", msg:{connectionId:timestamp}}));


    ws.on("message", function (data, flags) {
        console.log("websocket received a message");
        var clientMsg = data;
        wss.clients.forEach(function each(client) {
            client.send(clientMsg);
        });        
    });

    ws.on("close", function () {
        console.log("websocket connection close");
    });
});
console.log("websocket server created");

angularjs part:

.run(function ($rootScope) {
        if ("WebSocket" in window)
            {
               console.log("WebSocket is supported by your Browser!");
               $rootScope.updated = false;
               // Let us open a web socket
               socket = new WebSocket("ws://localhost:5000", "echo-protocol");
               socket.addEventListener("open", function(event) {
                  console.log( "Connected" );
                });

                // Display messages received from the server
                socket.addEventListener("message", function(event) {
                  console.log( "Server Says: " + event.data );

                  if (!$rootScope.updated)
                    if (event.data == "updated list") {
                      //here can passdata or update data
                      alert("Please refresh the page, new data should be available!");
                    }
                });

                // Display any errors that occur
                socket.addEventListener("error", function(event) {
                  console.log( "Error: " + event );
                });

                socket.addEventListener("close", function(event) {
                  console.log("Not Connected");
                });
            }

            else
            {
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
    })

然后运行"节点websocket.js" 和app" http-server -o"