diff --git a/BBWY.Client/BBWY.Client.csproj b/BBWY.Client/BBWY.Client.csproj index 71580690..0ff430af 100644 --- a/BBWY.Client/BBWY.Client.csproj +++ b/BBWY.Client/BBWY.Client.csproj @@ -44,6 +44,7 @@ + diff --git a/BBWY.Client/Helpers/DongDongHelper.cs b/BBWY.Client/Helpers/DongDongHelper.cs index 7f27c90a..e438f6b6 100644 --- a/BBWY.Client/Helpers/DongDongHelper.cs +++ b/BBWY.Client/Helpers/DongDongHelper.cs @@ -17,7 +17,7 @@ namespace BBWY.Client.Helpers public DongDongHelper() { csapi = new CSharpAPIs(); - inJectionTimer = new System.Timers.Timer(10000); + inJectionTimer = new System.Timers.Timer(20000); inJectionTimer.Elapsed += InJectionTimer_Elapsed; } diff --git a/BBWY.Client/Helpers/WSHelper.cs b/BBWY.Client/Helpers/WSHelper.cs deleted file mode 100644 index 55271a21..00000000 --- a/BBWY.Client/Helpers/WSHelper.cs +++ /dev/null @@ -1,59 +0,0 @@ -using BBWY.Common.Models; -using Fleck; -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Pipes; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace BBWY.Client.Helpers -{ - public class WSHelper : IDenpendency - { - //private List connectSocketPool = new List(); - - private WebSocketServer wsServer; - - public WSHelper() - { - wsServer = new WebSocketServer("ws://127.0.0.1:35192"); - } - - public Action ServerReceiveCallback; - - - public void Start() - { - //开启监听 - wsServer.Start(socket => - { - //注册客户端连接建立事件 - socket.OnOpen = () => - { - Console.WriteLine("建立连接"); - //将当前客户端连接对象放入连接池中 - //connectSocketPool.Add(socket); - }; - //注册客户端连接关闭事件 - socket.OnClose = () => - { - //Console.WriteLine("Close"); - //将当前客户端连接对象从连接池中移除 - //connectSocketPool.Remove(socket); - }; - //注册客户端发送信息事件 - socket.OnMessage = message => - { - Console.WriteLine($"收到webSocket消息:{message}"); - socket.Close(); - ////向客户端发送消息 - //socket.Send($"服务端接收到信息:{message}"); - ServerReceiveCallback?.Invoke(message); - }; - }); - } - } -} - diff --git a/BBWY.Client/ViewModels/Order/OrderListViewModel.cs b/BBWY.Client/ViewModels/Order/OrderListViewModel.cs index 75a9d810..1dd42200 100644 --- a/BBWY.Client/ViewModels/Order/OrderListViewModel.cs +++ b/BBWY.Client/ViewModels/Order/OrderListViewModel.cs @@ -160,7 +160,7 @@ namespace BBWY.Client.ViewModels /// public bool IsSDGroup { get => isSDGroup; set { Set(ref isSDGroup, value); } } - public OrderListViewModel(OrderService orderService, StatisticsService statisticsService, AfterOrderService afterOrderService, GlobalContext globalContext, ChoosePurchaseSchemeViewModel choosePurchaseSchemeViewModel, WSHelper pipeHelper, DongDongHelper dongdongHelper) + public OrderListViewModel(OrderService orderService, StatisticsService statisticsService, AfterOrderService afterOrderService, GlobalContext globalContext, ChoosePurchaseSchemeViewModel choosePurchaseSchemeViewModel, DongDongHelper dongdongHelper) { IsSDGroup = globalContext.User.TeamName == "刷单组"; if (IsSDGroup) @@ -174,7 +174,6 @@ namespace BBWY.Client.ViewModels } random = new Random(); - this.wsHelper = pipeHelper; this.dongdongHelper = dongdongHelper; this.globalContext = globalContext; this.orderService = orderService; @@ -537,7 +536,7 @@ namespace BBWY.Client.ViewModels if (string.IsNullOrEmpty(order.BuyerAccount)) { var buyerAccount = string.Empty; - var grab = new GrabJDMibole(order.Id); + var grab = new GrabJDMibole(order.Id, globalContext.User.Shop.ShopId.ToString()); if (grab.ShowDialog() == true) buyerAccount = grab.Tag.ToString(); if (string.IsNullOrEmpty(buyerAccount)) @@ -564,6 +563,12 @@ namespace BBWY.Client.ViewModels return; } + //建立websocket连接 + CloseWebSocket(); + webSocketClient = new WebSocketSharp.WebSocket($"ws://127.0.0.1:35192/bbwy/{order.BuyerAccount}", null) { }; + webSocketClient.OnMessage += WebSocketClient_OnMessage; + webSocketClient.Connect(); + //唤醒咚咚 dongdongHelper.WakeUpDongDong(waiter, order.BuyerAccount); } diff --git a/BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs b/BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs index e0108d49..25fd87fa 100644 --- a/BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs +++ b/BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs @@ -6,23 +6,39 @@ using System.Collections.Concurrent; using System.Linq; using System.Threading.Tasks; using System.Windows; +using WebSocketSharp; namespace BBWY.Client.ViewModels { public partial class OrderListViewModel { - private WSHelper wsHelper; private ConcurrentDictionary dongdongBuyerDictionary; + private WebSocket webSocketClient; + public void InitDongDong() { dongdongBuyerDictionary = new ConcurrentDictionary(); - wsHelper.ServerReceiveCallback = OnDongDongSendMessage; - wsHelper.Start(); } - private void OnDongDongSendMessage(string msg) + private void CloseWebSocket() + { + if (webSocketClient != null) + { + if (webSocketClient.IsAlive) + webSocketClient.Close(); + webSocketClient.OnMessage -= WebSocketClient_OnMessage; + webSocketClient = null; + } + } + + private void WebSocketClient_OnMessage(object sender, WebSocketSharp.MessageEventArgs e) { + if (!e.IsText) + return; + var msg = e.Data; + + CloseWebSocket(); Console.WriteLine($"OnDongDongSendMessage 接收到消息 {msg}"); if (string.IsNullOrEmpty(msg)) return; @@ -63,23 +79,6 @@ namespace BBWY.Client.ViewModels { Messenger.Default.Send(string.Empty, "ActiveMainWindow"); }); - ; - - //Task.Factory.StartNew(() => orderService.DecodeConsignee(order.Id, phone)).ContinueWith(t => - //{ - // var response = t.Result; - // IsLoading = false; - // if (!response.Success) - // { - // App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "解密失败")); - // return; - // } - // order.Consignee.ContactName = response.Data.ContactName; - // order.Consignee.Address = response.Data.Address; - // order.Consignee.Mobile = response.Data.Mobile; - // order.Consignee.IsDecode = true; - //}); - } catch (Exception ex) { @@ -87,4 +86,4 @@ namespace BBWY.Client.ViewModels } } } -} +} \ No newline at end of file diff --git a/BBWY.Client/Views/MainWindow.xaml.cs b/BBWY.Client/Views/MainWindow.xaml.cs index 73387f7d..2604d3cd 100644 --- a/BBWY.Client/Views/MainWindow.xaml.cs +++ b/BBWY.Client/Views/MainWindow.xaml.cs @@ -1,4 +1,5 @@ using BBWY.Controls; +using System; using System.Windows; namespace BBWY.Client.Views @@ -18,7 +19,11 @@ namespace BBWY.Client.Views private void MainWindow_Loaded(object sender, RoutedEventArgs e) { - GalaSoft.MvvmLight.Messaging.Messenger.Default.Register(this, "ActiveMainWindow", (x) => this.Activate()); + GalaSoft.MvvmLight.Messaging.Messenger.Default.Register(this, "ActiveMainWindow", (x) => + { + var a = this.Activate(); + Console.WriteLine(a); + }); } private void frame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) diff --git a/BBWY.Client/Views/Order/GrabJDMibole.xaml.cs b/BBWY.Client/Views/Order/GrabJDMibole.xaml.cs index 778da1c0..22ff3f2f 100644 --- a/BBWY.Client/Views/Order/GrabJDMibole.xaml.cs +++ b/BBWY.Client/Views/Order/GrabJDMibole.xaml.cs @@ -19,12 +19,14 @@ namespace BBWY.Client.Views.Order private WebView2Manager w2m; //private Stopwatch sw; private string orderId; + private string shopId; private bool isNavigated; - public GrabJDMibole(string orderId) + public GrabJDMibole(string orderId, string shopId) { InitializeComponent(); this.orderId = orderId; + this.shopId = shopId; this.Loaded += GrabJDMibole_Loaded; this.Unloaded += GrabJDMibole_Unloaded; } @@ -42,7 +44,7 @@ namespace BBWY.Client.Views.Order { w2m = s.ServiceProvider.GetRequiredService(); } - w2m.Init(); + w2m.Init(shopId); grid.Children.Add(w2m.wb2); if (w2m.IsInitializationCompleted) @@ -56,7 +58,7 @@ namespace BBWY.Client.Views.Order w2m.OnNavigationCompleted = async (e) => { if (w2m.wb2.CoreWebView2.Source.StartsWith("https://passport.shop.jd.com/login")) - { + { //首次打开需要登录 Console.WriteLine($"{orderId}触发登录"); await w2m.wb2.CoreWebView2.ExecuteScriptAsync(@$"var img = document.getElementById('js-login-qrcode'); diff --git a/BBWY.Client/WebView2Manager.cs b/BBWY.Client/WebView2Manager.cs index 8bd89399..063f7f82 100644 --- a/BBWY.Client/WebView2Manager.cs +++ b/BBWY.Client/WebView2Manager.cs @@ -15,12 +15,12 @@ namespace BBWY.Client //{ // Init(); //} - public void Init() + public void Init(string userDataFolder) { if (wb2 == null) { wb2 = new WebView2(); - var wb2Setting = CoreWebView2Environment.CreateAsync(userDataFolder: io.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WebView2UserData")).Result; + var wb2Setting = CoreWebView2Environment.CreateAsync(userDataFolder: io.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WebView2UserData", userDataFolder)).Result; wb2.EnsureCoreWebView2Async(wb2Setting); wb2.CoreWebView2InitializationCompleted += Wb2_CoreWebView2InitializationCompleted; wb2.NavigationCompleted += Wb2_NavigationCompleted; @@ -31,7 +31,7 @@ namespace BBWY.Client public Action OnWebMessageReceived; public Action OnNavigationCompleted; public Action CoreWebView2InitializationCompleted; - public bool IsInitializationCompleted { get ; private set; } + public bool IsInitializationCompleted { get; private set; } private void Wb2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e) { diff --git a/BBWY.WebSocket.Server/BBWY.WebSocket.Server.csproj b/BBWY.WebSocket.Server/BBWY.WebSocket.Server.csproj new file mode 100644 index 00000000..fc0eb2c8 --- /dev/null +++ b/BBWY.WebSocket.Server/BBWY.WebSocket.Server.csproj @@ -0,0 +1,16 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + + diff --git a/BBWY.WebSocket.Server/Helpers/WSHelper.cs b/BBWY.WebSocket.Server/Helpers/WSHelper.cs new file mode 100644 index 00000000..fe0fd149 --- /dev/null +++ b/BBWY.WebSocket.Server/Helpers/WSHelper.cs @@ -0,0 +1,71 @@ +using BBWY.Common.Models; +using Fleck; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Concurrent; + +namespace BBWY.WebSocket.Server.Helpers +{ + public class WSHelper : IDenpendency + { + private ConcurrentDictionary bbwySocketPool; + + private WebSocketServer wsServer; + + public WSHelper() + { + + } + + public void Listen() + { + wsServer = new WebSocketServer("ws://127.0.0.1:35192"); + bbwySocketPool = new ConcurrentDictionary(); + + //开启监听 + wsServer.Start(socket => + { + //注册客户端连接建立事件 + socket.OnOpen = () => + { + Console.WriteLine("建立连接"); + if (!string.IsNullOrEmpty(socket.ConnectionInfo.Path) && socket.ConnectionInfo.Path.Contains("bbwy")) + { + var key = socket.ConnectionInfo.Path.Substring(socket.ConnectionInfo.Path.LastIndexOf("/") + 1); + bbwySocketPool.TryRemove(key, out _); + bbwySocketPool.TryAdd(key, socket); //将bbwy当前客户端连接对象放入连接池中 + } + }; + //注册客户端连接关闭事件 + socket.OnClose = () => + { + if (!string.IsNullOrEmpty(socket.ConnectionInfo.Path) && socket.ConnectionInfo.Path.Contains("bbwy")) + { + var key = socket.ConnectionInfo.Path.Substring(socket.ConnectionInfo.Path.LastIndexOf("/") + 1); + bbwySocketPool.TryRemove(key, out _); + } + }; + //注册客户端发送信息事件 + socket.OnMessage = message => + { + Console.WriteLine($"收到webSocket消息:{message}"); + socket.Close(); + + try + { + var j = JObject.Parse(message); + if (!string.IsNullOrEmpty(j.Value("user"))) + { + if (bbwySocketPool.TryGetValue(j.Value("user"), out IWebSocketConnection bbwySocket)) + { + _ = bbwySocket.Send(message); + } + } + } + catch { } + }; + }); + } + } +} + diff --git a/BBWY.WebSocket.Server/Program.cs b/BBWY.WebSocket.Server/Program.cs new file mode 100644 index 00000000..e0cb2fdb --- /dev/null +++ b/BBWY.WebSocket.Server/Program.cs @@ -0,0 +1,17 @@ +using BBWY.WebSocket.Server.Helpers; +using System; + +namespace BBWY.WebSocket.Server +{ + internal class Program + { + private static WSHelper wSHelper; + + static void Main(string[] args) + { + wSHelper = new WSHelper(); + wSHelper.Listen(); + Console.ReadKey(); + } + } +} diff --git a/BBWY.sln b/BBWY.sln index 2a707e0a..f286e577 100644 --- a/BBWY.sln +++ b/BBWY.sln @@ -40,6 +40,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PJZS", "PJZS\PJZS.csproj", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InDll2", "InDll2\InDll2.csproj", "{1FD36D27-9BED-4E72-B81C-CBC0C4A3120E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BBWY.WebSocket.Server", "BBWY.WebSocket.Server\BBWY.WebSocket.Server.csproj", "{4E08D6A8-5670-4890-B775-2A10017201FD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -152,6 +154,14 @@ Global {1FD36D27-9BED-4E72-B81C-CBC0C4A3120E}.Release|Any CPU.Build.0 = Release|Any CPU {1FD36D27-9BED-4E72-B81C-CBC0C4A3120E}.Release|x86.ActiveCfg = Release|Any CPU {1FD36D27-9BED-4E72-B81C-CBC0C4A3120E}.Release|x86.Build.0 = Release|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Debug|x86.ActiveCfg = Debug|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Debug|x86.Build.0 = Debug|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Release|Any CPU.Build.0 = Release|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Release|x86.ActiveCfg = Release|Any CPU + {4E08D6A8-5670-4890-B775-2A10017201FD}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -169,6 +179,7 @@ Global {3D37A8D3-0364-4539-B3E9-A6610F4659A8} = {B545F3FA-E6A7-4CB1-B92D-801C66357CF5} {FB4BF1A9-DA81-446F-903C-89D3F905B7B8} = {D097AEA5-A7B1-414D-B82D-F70F177D0D8D} {1FD36D27-9BED-4E72-B81C-CBC0C4A3120E} = {D097AEA5-A7B1-414D-B82D-F70F177D0D8D} + {4E08D6A8-5670-4890-B775-2A10017201FD} = {B545F3FA-E6A7-4CB1-B92D-801C66357CF5} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {CC045257-4D86-45FD-A1F0-2715C024F1B5}