Browse Source

bbwy客户端对接websocket

qianyi
shanji 2 years ago
parent
commit
9152b159b4
  1. 1
      BBWY.Client/BBWY.Client.csproj
  2. 2
      BBWY.Client/Helpers/DongDongHelper.cs
  3. 59
      BBWY.Client/Helpers/WSHelper.cs
  4. 11
      BBWY.Client/ViewModels/Order/OrderListViewModel.cs
  5. 41
      BBWY.Client/ViewModels/Order/OrderListVoewModel_DongDong.cs
  6. 7
      BBWY.Client/Views/MainWindow.xaml.cs
  7. 6
      BBWY.Client/Views/Order/GrabJDMibole.xaml.cs
  8. 6
      BBWY.Client/WebView2Manager.cs
  9. 16
      BBWY.WebSocket.Server/BBWY.WebSocket.Server.csproj
  10. 71
      BBWY.WebSocket.Server/Helpers/WSHelper.cs
  11. 17
      BBWY.WebSocket.Server/Program.cs
  12. 11
      BBWY.sln

1
BBWY.Client/BBWY.Client.csproj

@ -44,6 +44,7 @@
<PackageReference Include="NLog" Version="4.7.12" />
<PackageReference Include="NPOI" Version="2.6.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
<PackageReference Include="WebSocketSharp" Version="1.0.3-rc11" />
</ItemGroup>
<ItemGroup>

2
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;
}

59
BBWY.Client/Helpers/WSHelper.cs

@ -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<IWebSocketConnection> connectSocketPool = new List<IWebSocketConnection>();
private WebSocketServer wsServer;
public WSHelper()
{
wsServer = new WebSocketServer("ws://127.0.0.1:35192");
}
public Action<string> 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);
};
});
}
}
}

11
BBWY.Client/ViewModels/Order/OrderListViewModel.cs

@ -160,7 +160,7 @@ namespace BBWY.Client.ViewModels
/// </summary>
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);
}

41
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<string, string> dongdongBuyerDictionary;
private WebSocket webSocketClient;
public void InitDongDong()
{
dongdongBuyerDictionary = new ConcurrentDictionary<string, string>();
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)
{

7
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<string>(this, "ActiveMainWindow", (x) => this.Activate());
GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<string>(this, "ActiveMainWindow", (x) =>
{
var a = this.Activate();
Console.WriteLine(a);
});
}
private void frame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)

6
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<WebView2Manager>();
}
w2m.Init();
w2m.Init(shopId);
grid.Children.Add(w2m.wb2);
if (w2m.IsInitializationCompleted)

6
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<CoreWebView2WebMessageReceivedEventArgs> OnWebMessageReceived;
public Action<CoreWebView2NavigationCompletedEventArgs> OnNavigationCompleted;
public Action<CoreWebView2InitializationCompletedEventArgs> CoreWebView2InitializationCompleted;
public bool IsInitializationCompleted { get ; private set; }
public bool IsInitializationCompleted { get; private set; }
private void Wb2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{

16
BBWY.WebSocket.Server/BBWY.WebSocket.Server.csproj

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fleck" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BBWY.Common\BBWY.Common.csproj" />
</ItemGroup>
</Project>

71
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<string, IWebSocketConnection> bbwySocketPool;
private WebSocketServer wsServer;
public WSHelper()
{
}
public void Listen()
{
wsServer = new WebSocketServer("ws://127.0.0.1:35192");
bbwySocketPool = new ConcurrentDictionary<string, IWebSocketConnection>();
//开启监听
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<string>("user")))
{
if (bbwySocketPool.TryGetValue(j.Value<string>("user"), out IWebSocketConnection bbwySocket))
{
_ = bbwySocket.Send(message);
}
}
}
catch { }
};
});
}
}
}

17
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();
}
}
}

11
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}

Loading…
Cancel
Save