.net websocket服务端可以通过安装System.Net.WebSockets库实现,创建WebSocketServer类并继承自ClientWebSocket类。
WebSocket简介 WebSocket是一种在单个TCP连接上进行全双工通信的协议,它使得客户端和服务器之间可以实时双向传输数据,而不需要频繁地建立和关闭连接,WebSocket在.NET中的实现主要依赖于System.Net.WebSockets命名空间。
创新互联专注于桃源网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供桃源营销型网站建设,桃源网站制作、桃源网页设计、桃源网站官网定制、小程序定制开发服务,打造桃源网络公司原创品牌,更为您提供桃源网站排名 全网营销落地服务。
安装WebSocket库 在使用WebSocket之前,需要先安装相应的库,在项目中,可以通过NuGet包管理器来安装Microsoft.AspNetCore.WebSockets包。
创建WebSocket服务器 1、引入命名空间:
using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
2、创建一个WebSocket服务器类:
public class WebSocketServer
{
private readonly HttpListener _listener;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
public WebSocketServer(string url)
{
_listener = new HttpListener();
_listener.Prefixes.Add(url);
_listener.Start();
}
public async Task Start()
{
while (!_cancellationTokenSource.IsCancellationRequested)
{
var context = await _listener.GetContextAsync();
if (context != null && context.RequestType == "CONNECT")
{
await ProcessRequest(context);
}
}
}
private async Task ProcessRequest(HttpListenerContext context)
{
var webSocketContext = await context.AcceptWebSocketAsync(null);
if (webSocketContext != null)
{
// 处理WebSocket连接请求
await ProcessWebSocketConnection(webSocketContext);
}
}
private async Task ProcessWebSocketConnection(HttpListenerWebSocketContext webSocketContext)
{
try
{
// 读取客户端发送的消息并广播给所有连接的客户端
using (var buffer = new byte[1024])
{
var bytesRead = await webSocketContext.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None);
if (bytesRead > 0)
{
var message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
await SendMessageToAllClients(message);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
webSocketContext.Abort(); // 关闭WebSocket连接
}
}
}
创建WebSocket客户端(HTML页面) 1、引入JavaScript库: 和 ,这两个库分别用于处理WebSocket连接和消息传递。
2、编写HTML代码:
WebSocket Client
Send
基本
文件
流程
错误
SQL
调试
请求信息 : 2026-02-17 03:14:08 HTTP/1.1 GET : /article/coopooc.html 运行时间 : 0.0680s ( Load:0.0032s Init:0.0005s Exec:0.0589s Template:0.0054s ) 吞吐率 : 14.71req/s 内存开销 : 2,216.15 kb 查询信息 : 12 queries 5 writes 文件加载 : 36 缓存信息 : 0 gets 2 writes 配置加载 : 130 会话信息 : SESSION_ID=mttp5gpurnf575ekisdpfju9j3
/home/wwwroot/jxjierui.cn/index.php ( 1.12 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB ) /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB ) /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB ) /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB ) /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB ) /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB ) /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB ) /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
[ app_init ] --START-- Run Behavior\BuildLiteBehavior [ RunTime:0.000004s ] [ app_init ] --END-- [ RunTime:0.000021s ] [ app_begin ] --START-- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000146s ] [ app_begin ] --END-- [ RunTime:0.000159s ] [ view_parse ] --START-- [ template_filter ] --START-- Run Behavior\ContentReplaceBehavior [ RunTime:0.000079s ] [ template_filter ] --END-- [ RunTime:0.000102s ] Run Behavior\ParseTemplateBehavior [ RunTime:0.004180s ] [ view_parse ] --END-- [ RunTime:0.004206s ] [ view_filter ] --START-- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000076s ] [ view_filter ] --END-- [ RunTime:0.000088s ] [ app_end ] --START--
1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= ) [8] Undefined index: pid /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行. [2] file_put_contents(./App/Runtime/Temp/c76735bd9445c4362abd73d34181c447.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行. [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行. [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行. [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行. [2] file_put_contents(./App/Runtime/Temp/563ce5827b7c7839031019e57233dc2d.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
0.0680s