/// <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); }); }
/// <summary> /// 认证授权KEY /// </summary> /// <param name="license"></param> /// <returns></returns> public static Response AuthLicense(string license) { try { var response = Post("auth_license_key", new JObject { ["license_key"] = license }); if (response.Result) { License = new License { Avatra = response.Json["user"]?["t_avatar"]?.ToString(), Email = response.Json["user"]?["t_email"]?.ToString(), Id = (int)response.Json["user"]?["t_id"], LicenseCustom = response.Json["license"]?["t_custom"]?.ToString(), LicenseExpiration = ConvertTimestamp((double)response.Json["license"]?["t_expiration_time"]), LicenseKey = response.Json["user"]?["t_license_key"]?.ToString(), LicenseLevel = (int)response.Json["license"]?["t_level"], LicenseName = response.Json["license"]?["t_name"]?.ToString(), Name = response.Json["user"]?["t_name"]?.ToString(), Phone = response.Json["user"]?["t_phone"]?.ToString(), Session = response.Json["session"]?.ToString() }; //释放资源 CoreSocket?.Disconnect(); CoreSocket?.Off(); CoreSocket?.Close(); var options = new IO.Options { IgnoreServerCertificateValidation = false, AutoConnect = true, ForceNew = true, Query = new Dictionary <string, string> { { "action", "client" }, { "session", License.Session } }, Path = "/xky", Transports = ImmutableList.Create("websocket") }; CoreSocket = IO.Socket("wss://api.xky.com", options); CoreSocket.On(Socket.Client.Socket.EventConnect, () => { Console.WriteLine("Connected"); CoreConnected = true; }); CoreSocket.On(Socket.Client.Socket.EventDisconnect, () => { Console.WriteLine("Disconnected"); CoreConnected = false; }); CoreSocket.On(Socket.Client.Socket.EventError, () => { Console.WriteLine("ERROR"); }); CoreSocket.On("event", json => { CoreEvent((JObject)json); }); } else { License = null; } return(response); } catch (Exception e) { return(new Response { Result = false, Message = e.Message, Json = new JObject { ["errcode"] = 1, ["msg"] = e.Message } }); } }
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); }); }