KRPano-screen interaction principle

KRPano-screen interaction principle

KRPano synchronization between multiple screen display can be realized, mainly applied to Websocket technology for communication.

 

On the control side, we need to send the KRPano scene perspective and scene information in real time, you can use the following code:

            embedpano({
                swf: "tour.swf",
                xml: "tour.xml",
                target: "pano",
                html5: "auto",
                mobilescale: 1.0,
                passQueryParameters: true,
                onready: krpanoReady
            });

            function krpanoReady(krpano) {
                krpano.call("trace(krpano is ready...)");
                krpano.call("loadscene(scene_04, null, MERGE);")
                initialWebSocket();
            }
            Send KRPano perspective information in real time
            function IntervalSendMessage() {
                var krpano = document.getElementById("krpanoSWFObject");

                if (krpano && krpano.get) {
                    var hlookat = krpano.get("view.hlookat");
                    var vlookat = krpano.get("view.vlookat");
                    var fov = krpano.get("view.fov");
                    var scene = krpano.get("xml.scene");

                    var krObj = {
                        hlookat: hlookat,
                        vlookat: vlookat,
                        fov: fov,
                        scene: scene
                    }

                    ws.send(JSON.stringify(krObj));
                }
            }
            var ws;
            var WebSocketsExist;

            function initialWebSocket() {
                WebSocketsExist = true;
                try {
                    ws = new WebSocket("ws://localhost:8989/");
                } catch (ex) {
                    try {
                        ws = new MozWebSocket("ws://localhost:8989/");
                    } catch (ex) {
                        WebSocketsExist = false;
                    }
                }
                if (WebSocketsExist) {
                    console.log("The current browser support websocket!");
                } else {}
                    console.log("The current browser not support websocket!")
                    return;
                }
                ws.onopen = WSonOpen;
                ws.onmessage = WSonMessage;
                ws.onclose = WSonClose;
                ws.onerror = WSonError;
            }

            function WSonOpen() {
                console.log("websocket opened success!");
                setInterval(IntervalSendMessage, 50);
            };

            function WSonMessage(event) {
                console.log(event.data);
            };

            function WSonClose() {
                console.log("Websocket closed.");
            };

            function WSonError() {
                console.log("Websocket error occur.");
            };

 

On the control side, we need to receive real-time control over the perspective of information and periodic updates to the current perspective and scene.

            embedpano({
                swf: "tour.swf",
                xml: "tour.xml",
                target: "pano",
                html5: "auto",
                mobilescale: 1.0,
                passQueryParameters: true
            });

            const WebSocket = require('ws');

            const wss = new WebSocket.Server({
                port: 8989
            });

            wss.on('connection', function connection(ws) {
                ws.on('message', function incoming(message) {
                    console.log('received: %s', message);
                    krObj = JSON.parse(message);
                    var hlookat = krObj.hlookat;
                    var vlookat = krObj.vlookat;
                    var fov = krObj.fov;
                    var scene = krObj.scene;
                    var krpano = document.getElementById("krpanoSWFObject");
                    if (krpano && krpano.set) {
                        krpano.call("loadscene(get(" + "scene_04" + "), null, MERGE);")
                        krpano.set("view.hlookat", hlookat);
                        krpano.set("view.vlookat", vlookat);
                        krpano.set("view.fov", fov);
                        if (krpano.get("xml.scene") !== scene) {
                            loadscene(get(startscene), null, MERGE);
                            krpano.call("loadscene(" + scene + ", null, MERGE);")
                        }
                    }
                });

                ws.send('something');
            });

 

If the control port (such as a Web page) cannot be directly creating a WebSocket. Server, you can use the server for relay control Terminal and control terminal are connected to the server, server side state control information to the console.

 

This post was published on: http://www. krpano. tech/archives/534

Posted by: dragon slayer

Reprinted please specify the source, thank you!