예제 #1
0
        private void setup()
        {
            cache     = new Dictionary <string, object>();
            cacheKeys = new List <string>();

            server.getStream((success, response) =>
            {
                if (!success)
                {
                    OnError?.Invoke(this, new SpeckleEventArgs("Failed to retrieve stream."));
                    return;
                }
                streamFound        = true;
                streamLiveInstance = response; // this will fail!!!
                setupWebsocket();
            });

            // ready is defined as: streamId exists && wsSessionId && streamWas found.
            isReadyCheck           = new System.Timers.Timer(150);
            isReadyCheck.AutoReset = false; isReadyCheck.Enabled = true;
            isReadyCheck.Elapsed  += (sender, e) =>
            {
                if (server.streamId == null)
                {
                    isReadyCheck.Start(); return;
                }
                if (server.wsSessionId == null)
                {
                    isReadyCheck.Start(); return;
                }
                if (!streamFound)
                {
                    isReadyCheck.Start(); return;
                }

                getObjects(streamLiveInstance as ExpandoObject, (castObjects) =>
                {
                    dynamic eventData = new ExpandoObject();
                    eventData.objects = castObjects;
                    eventData.layers  = SpeckleLayer.fromExpandoList(streamLiveInstance.layers);
                    eventData.name    = streamLiveInstance.name;

                    OnReady?.Invoke(this, new SpeckleEventArgs("receiver ready", eventData));
                });
            };
        }
예제 #2
0
        private void setupWebsocket()
        {
            wsReconnecter           = new System.Timers.Timer(reconnDebounceInterval);
            wsReconnecter.AutoReset = false; wsReconnecter.Enabled = false;
            wsReconnecter.Elapsed  += (sender, e) =>
            {
                ws.Connect();
            };

            ws = new WebSocket(server.wsEndpoint + "?access_token=" + server.token);

            ws.OnOpen += (sender, e) =>
            {
                wsReconnecter.Stop();
                ws.Send(JsonConvert.SerializeObject(new { eventName = "join-stream", args = new { streamid = server.streamId, role = "receiver" } }));
            };

            ws.OnClose += (sender, e) =>
            {
                wsReconnecter.Start();
                server.wsSessionId = null;
            };

            ws.OnMessage += (sender, e) =>
            {
                if (e.Data == "ping")
                {
                    ws.Send("alive");
                    return;
                }

                dynamic message = JsonConvert.DeserializeObject <ExpandoObject>(e.Data);

                if (message.eventName == "ws-session-id")
                {
                    server.wsSessionId = message.sessionId;
                    return;
                }

                if (message.eventName == "volatile-broadcast")
                {
                    OnBroadcast?.Invoke(this, new SpeckleEventArgs("volatile-broadcast", message));
                    return;
                }

                if (message.eventName == "volatile-message")
                {
                    OnMessage?.Invoke(this, new SpeckleEventArgs("volatile-message", message));
                    return;
                }

                if (message.eventName == "live-update")
                {
                    OnDataMessage?.Invoke(this, new SpeckleEventArgs("Received update notification."));
                    getObjects(message.args as ExpandoObject, (castObjects) =>
                    {
                        dynamic eventData = new ExpandoObject();
                        eventData.objects = castObjects;
                        eventData.layers  = SpeckleLayer.fromExpandoList(message.args.layers);
                        eventData.name    = message.args.name;

                        OnData?.Invoke(this, new SpeckleEventArgs("live-update", eventData));
                    });
                    return;
                }

                if (message.eventName == "metadata-update")
                {
                    dynamic eventData = new ExpandoObject();
                    eventData.name   = message.args.name;
                    eventData.layers = SpeckleLayer.fromExpandoList(message.args.layers);
                    OnMetadata?.Invoke(this, new SpeckleEventArgs("metadata-update", eventData));
                    return;
                }

                if (message.eventName == "history-update")
                {
                    // TODO
                    OnHistory?.Invoke(this, new SpeckleEventArgs("history-update"));
                    return;
                }
            };

            ws.Connect();
        }