.net core Fleck WebSocket使用笔记
@@.net core Fleck
socket帮助类
using Fleck; using KOTL_EvidenceService.Model; using System; using System.Collections.Generic; namespace KOTL_EvidenceService.Util { public class ServerHelper { WebSocketServer server = null; SessionEntiy session = new SessionEntiy(); public ServerHelper() { server = new WebSocketServer("ws://192.168.0.36:7010");//监听所有的的地址 //出错后进行重启 server.RestartAfterListenError = true; } public void Start() { //开始监听 server.Start(socket => { socket.OnOpen = () => OnOpen(socket); socket.OnClose = () => OnClose(socket); socket.OnMessage = message => //接受客户端网页消息事件 { string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort; Console.WriteLine(DateTime.Now.ToString() + "|服务器:【收到】来客户端网页:" + clientUrl + "的信息:\n" + message); }; }); } public void OnOpen(IWebSocketConnection socket) { //获取客户端网页的url string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort; Console.WriteLine(DateTime.Now.ToString() + "|服务器:和客户端网页:" + clientUrl + " 建立WebSock连接!"); } //连接关闭事件 public void OnClose(IWebSocketConnection socket) { string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort; Console.WriteLine(DateTime.Now.ToString() + "|服务器:和客户端网页:" + clientUrl + " 断开WebSock连接!"); } } } 调用 ServerHelper websocket = new ServerHelper(); websocket.Start();
————————————————
版权声明:本文为CSDN博主「qq_40580931」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40580931/article/details/120781798
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2022-08-11 《Redis核心技术与实战》学习总结(18)
2022-08-11 C#中using的使用的几种场景
2014-08-11 evernote