.net framework websocket连接.net coreSignalR服务器。
作者:互联网
突然用.net core做api接口了 ,电脑上的程序用.net framework开发的,
网上非signalr客户端连接的中文文档就特么几个,找了半天终于搞了出来,必须精简简单
代码引用 WebSocket4Net; 直接nuget下载
using WebSocket4Net; public static WebSocket webSocket4Net = null; static void Main(string[] args) { Console.WriteLine("客户端"); webSocket4Net = new WebSocket("ws://192.168.2.110:56091/hub"); webSocket4Net.Opened += WebSocket4Net_Opened; webSocket4Net.MessageReceived += WebSocket4Net_MessageReceived; webSocket4Net.Open(); //下面的就是调用服务端的方式了 var data = @"{ ""type"": 1, ""streamIds"":[], ""invocationId"": ""0"", ""target"": ""你的服务端的方法"", ""arguments"": [ ""服务端方法的值"" ] }" + "\u001e";//发送远程调用 log方法 webSocket4Net.Send(data); Console.WriteLine("按任意键结束。。。"); Console.ReadKey(); webSocket4Net.Dispose(); } private static void WebSocket4Net_MessageReceived(object sender, MessageReceivedEventArgs e) { JObject jobj = JObject.Parse(e.Message.Replace("\u001e", "")); if (jobj["target"]?.ToString() == "CallerBooking") { JArray arguments = JArray.Parse(jobj["arguments"].ToString()); string data = arguments.First.ToString(); } Console.WriteLine($"服务端回复数据:{e.Message}!"); } private static void WebSocket4Net_Opened(object sender, EventArgs e) { var data = @"{""protocol"":""json"", ""version"":1}" + "\u001e"; webSocket4Net.Send(data); Console.WriteLine("客户端连接成功"); }
标签:websocket,Console,coreSignalR,static,arguments,WebSocket4Net,webSocket4Net,net,d 来源: https://www.cnblogs.com/xxxin/p/16670488.html