您的位置:首页 > 编程语言 > ASP

asp.net core signalR

2018-06-04 19:13 721 查看

一、asp.net core使用signalR入门

1,nuget Microsoft.AspNetCore.SignalR 

2,新建ChatHub文件

using System;
using Microsoft.AspNetCore.SignalR.Client;
using System.Threading.Tasks;
using System.Threading;

namespace SignalRClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("准备就绪...");

HubConnection connection=new HubConnectionBuilder()
.WithUrl("http://localhost:5000/chatHub")
.Build();

//连接hub
connection.StartAsync();
Console.WriteLine("已连接");

//定义一个客户端方法ReceiveMessage
connection.On<string,string>("ReceiveMessage",(UriParser,message)=>{
Console.WriteLine($"接收消息:{message}");
});

while(true)
{
Thread.Sleep(1000);
var msg=Console.ReadLine();
//发送给hub的SendMessage方法
connection.InvokeAsync("SendMessage","",msg);
}

}
}

}
Main

案例下载:https://pan.baidu.com/s/1gqGNl_FPXxz0LYVozrqI5g

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: