Example #1
0
        /// <summary>
        /// 连接设备
        /// </summary>
        /// <param name="model"></param>
        public void Connect(Device model)
        {
            if (_decoder == null)
            {
                _decoder = new H264Decoder();
                _decoder.OnDecodeBitmapSource += Decoder_OnDecodeBitmapSource;
            }

            if (CurrentDevice != null && model.Sn != CurrentDevice.Sn)
            {
                Dispatcher.Invoke(() =>
                {
                    if (CurrentDevice.ScreenShot != null)
                    {
                        CurrentDevice.ScreenShot = CurrentDevice.ScreenShot.Clone();
                    }
                });
            }

            AddLabel("正在连接设备[" + model.Name + "]..", Colors.GreenYellow);
            CurrentDevice = Client.GetDevice(model.Sn);


            if (CurrentDevice == null)
            {
                throw new Exception("无法获取这个设备的信息");
            }
            var node = Client.Nodes.ToList().Find(p => p.Serial == CurrentDevice.NodeSerial);

            if (node != null)
            {
                CurrentDevice.NodeUrl = node.NodeUrl;
                Console.WriteLine("连接屏幕:" + CurrentDevice.NodeUrl);
            }

            if (CurrentDevice.NodeUrl == "")
            {
                AddLabel("设备所属节点服务器尚未连接,请稍后...", Colors.OrangeRed); throw new Exception("该设备没有设置P2P转发模式且尚未在局域网中发现节点服务器");
            }


            _socket?.Disconnect();
            _socket?.Close();

            //重置
            _bindingSource = 0;
            _isShow        = false;

            var options = new IO.Options
            {
                IgnoreServerCertificateValidation = true,
                AutoConnect = true,
                ForceNew    = true,
                Query       = new Dictionary <string, string>
                {
                    { "sn", CurrentDevice.Sn },
                    { "action", "mirror" },
                    { "v2", "true" },
                    { "hash", CurrentDevice.ConnectionHash }
                },
                Path       = "/xky",
                Transports = ImmutableList.Create("websocket")
            };

            _socket = IO.Socket(CurrentDevice.NodeUrl, options);
            _socket.On(Socket.Client.Socket.EventConnect, () => { Console.WriteLine("Connected"); });
            _socket.On(Socket.Client.Socket.EventDisconnect, () => { Console.WriteLine("Disconnected"); });
            _socket.On(Socket.Client.Socket.EventError, () => { Console.WriteLine("ERROR"); });
            _socket.On("event", json => { Console.WriteLine(json); });
            _socket.On("h264", data => { _decoder?.Decode((byte[])data); });
        }
Example #2
0
        public void Connect(Device model)
        {
            if (_decoder == null)
            {
                _decoder = new H264Decoder();
                _decoder.OnDecodeBitmapSource += Decoder_OnDecodeBitmapSource;
            }

            if (_device != null && model.Sn != _device.Sn)
            {
                Dispatcher.Invoke(() =>
                {
                    if (_device.ScreenShot != null)
                    {
                        _device.ScreenShot = _device.ScreenShot.Clone();
                    }
                });
            }

            AddLabel("正在获取设备" + model.Sn + "的连接信息..", Colors.White);
            _device = Client.GetDevice(model.Sn);


            if (_device == null)
            {
                throw new Exception("无法获取这个设备的信息");
            }

            if (Client.LocalNodes.ContainsKey(_device.NodeSerial))
            {
                _device.NodeUrl = "http://" + Client.LocalNodes[_device.NodeSerial].Ip + ":8080";
                Console.WriteLine(_device.NodeUrl);
            }


            if (_device.NodeUrl == "")
            {
                throw new Exception("该设备没有设置P2P转发模式");
            }


            _socket?.Disconnect();
            _socket?.Close();

            //重置
            _bindingSource = 0;
            _isShow        = false;

            var options = new IO.Options
            {
                IgnoreServerCertificateValidation = true,
                AutoConnect = true,
                ForceNew    = true,
                Query       = new Dictionary <string, string>
                {
                    { "sn", _device.Sn },
                    { "action", "mirror" },
                    { "v2", "true" },
                    { "hash", _device.ConnectionHash }
                },
                Path       = "/xky",
                Transports = ImmutableList.Create("websocket")
            };

            AddLabel("正在连接..", Colors.White);
            _socket = IO.Socket(_device.NodeUrl, options);
            _socket.On(Socket.Client.Socket.EventConnect, () => { Console.WriteLine("Connected"); });
            _socket.On(Socket.Client.Socket.EventDisconnect, () => { Console.WriteLine("Disconnected"); });
            _socket.On(Socket.Client.Socket.EventError, () => { Console.WriteLine("ERROR"); });
            _socket.On("event", json => { Console.WriteLine(json); });
            _socket.On("h264", data => { _decoder?.Decode((byte[])data); });
        }