1111
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="WebSocketSharp-NetStandard" Version="1.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,40 @@
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Server;
|
||||
|
||||
namespace ConsoleApp1
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var wssv = new WebSocketServer("ws://0.0.0.0:8080");
|
||||
|
||||
wssv.AddWebSocketService<WsHandler>("/ws");
|
||||
|
||||
wssv.Start();
|
||||
Console.WriteLine("WebSocket服务器已启动 ws://localhost:8080/ws");
|
||||
|
||||
Console.ReadKey();
|
||||
wssv.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
// 处理连接
|
||||
public class WsHandler : WebSocketBehavior
|
||||
{
|
||||
protected override void OnOpen()
|
||||
{
|
||||
Console.WriteLine("客户端连接");
|
||||
}
|
||||
|
||||
protected override void OnMessage(MessageEventArgs e)
|
||||
{
|
||||
Console.WriteLine($"收到消息: {e.Data}");
|
||||
}
|
||||
|
||||
protected override void OnClose(CloseEventArgs e)
|
||||
{
|
||||
Console.WriteLine("客户端断开");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user