.net5 websocket 客户端

发布时间 2023-10-27 17:22:27作者: Hey,Coder!

string url = "http://127.0.0.1:2141";
HubConnection _conn = new HubConnection(url, true);
IHubProxy _proxy = _conn.CreateHubProxy($"/MyHub");

_conn.Start();

//定义客户端的方法sendMessage()(有两个string类型的参数,当服务端调用sendMessage,需要传入2个string类型参数),以这种格式定义方法服务端才能去调用
_proxy.On<string, string>("sendMessage", (n, s) =>
{

});

_conn.StateChanged += new Action(tgt =>
{
if (((StateChange)tgt).NewState == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
{
//t1 = Environment.TickCount;
//客户端调用服务端的 Send() 方法,传入参数"Hello"
_proxy.Invoke("send", "Hello");
}
});