c#WebSocket
2020-12-22 19:27
阅读:653
YPE HTML>
标签:input void and each style 相互 网上 listen creat
最近需要WebSocket,就去网上找了Demo
先附地址:https://www.cnblogs.com/sheseido/p/7047948.html
先是Html端的代码,也算客户端吧:新建一个WebApplication空项目就行了,然后添加要给Html页,设未起始页
span>"-//W3C//DTD HTML 4.0 Transitional//EN">websocket client
"incomming">
然后就是服务端:一个控制台应用程序即可
using Fleck; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace websocket { class Program { static void Main(string[] args) { FleckLog.Level = LogLevel.Debug; var allSockets = new List(); var server = new WebSocketServer("ws://0.0.0.0:7181"); server.Start(socket => { socket.OnOpen = () => { Console.WriteLine("Open!"); allSockets.Add(socket); }; socket.OnClose = () => { Console.WriteLine("Close!"); allSockets.Remove(socket); }; socket.OnMessage = message => { Console.WriteLine(message); allSockets.ToList().ForEach(s => s.Send("Echo: " + message)); }; }); var input = Console.ReadLine(); while (input != "exit") { foreach (var socket in allSockets.ToList()) { socket.Send(input); } input = Console.ReadLine(); } } } }
然后先运行服务端,在运行客户端,就可以相互发送消息了
c#WebSocket
标签:input void and each style 相互 网上 listen creat
原文地址:https://www.cnblogs.com/fanlin92/p/13626109.html
评论
亲,登录后才可以留言!