12 changed files with 160 additions and 92 deletions
@ -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); |
|||
}; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
|
@ -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> |
@ -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 { } |
|||
}; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
|
@ -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(); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue