本文主要介绍宇舶表宇舶表官网(湖北人 s考网-官网),下面一起看看宇舶表宇舶表官网(湖北人 s考网-官网)相关资讯。
public class str:中心{ public channel reader string readlogstream{ var channel = channel。createunboundedstring;_ = writefileline(通道。作家);返回频道。读者;}私有异步任务write fileline(channelwriterstring writer){ using(var streamreader = new streamreader(directory。getcurrentdirectory /logs/server . log )){ string linewhile ((line = streamreader。readline)!= null) { await writer。write async(line);等待任务。延迟(时间跨度。fromseconds(2))。}作家。try complete;} } }使用微软。aspnetcore . authorization使用微软。aspnetcore . signalr使用系统;使用系统。集合。泛型;使用系统。linq使用系统。线程。任务;命名空间signalrcharsample。hubs {[authorize]public class userchathub: hub { public async task sendmessage(string to username,string message) { await clients。用户(tousername)。send async( 接收消息 ,$ {日期时间。现在}-{上下文。用户标识符}-{消息} );} }}
!doctype html html head meta charset = utf-8和/ title/title/headbody div用户名:输入类型= 文本 id = 用户输入 / message:输入类型= 文本 id = 消息输入 /input type = 按钮 id = 发送按钮 价值= 发送 //div ul id = 信息列表/ ul脚本src = http://localhost: 57634/lib/jquery/dist/jquery . js /script脚本src = http://localhost: 57634/lib/signalr . min . js /script脚本变量连接=新信号r。hubconnectionbuilder。with url( http://localhost: 57634/chathub ).build;connection . on( 接收消息 ,函数(用户,消息){ var msg =用户 说 消息;var li = document.createelement李 );li.textcontent = msgdocument . getelementbyid( 信息列表).appendchild(李);});connection.start。catch(function(err){ return console . error(err . tostring);});document . getelementbyid( 发送按钮 ).addevent listener( 点击 ,function(event){ var user = document . getelementbyid( 用户输入 ).价值;var消息=document . getelementbyid( 消息输入 ).价值;connection . invoke(发送消息 用户、消息)。catch(function(err){ return console . error(err . tostring);});event . prevent default;});/script/body/html
使用微软。aspnetcore . signalr使用系统;使用系统。集合。泛型;使用系统。linq使用系统。线程。任务;命名空间signalrcharsample。hubs { public class chathub : hub { public async task sendmessage(string user,string message) { await clients。send async( 接收消息 ,用户,日期时间。现在 - 消息);}公共任务sendmessagetocaller(字符串消息){返回客户端。send async( 接收消息 ,消息);}公共任务sendmessagetogroups(string message){ liststring groups = new liststring{ 第一组 , 第二组 };回头客。团体(团体)。send async( 接收消息 ,消息);} public override async task onconnectedasync{ await groups。addtogroupasync(上下文。connection id 第一组 );等待基地。onconnectedasync;}公共o在disconnectedasync上覆盖异步任务(异常异常){ await groups。removefromgroupasync(上下文。connection id 第一组 );等待基地。ondisconnectedasync(异常);} } }使用微软。aspnetcore . signalr使用系统;使用系统。集合。泛型;使用系统。linq使用系统。线程。任务;命名空间signalrcharsample。hubs { public class custom us: iuseridprovider { public string get userid(hubconnectioncontext connection){//返回连接。gethttpcontextrequest . query[ 用户 ];回路连接。用户?。findfirst( 用户名和密码)?。价值;} } }使用微软。aspnetcore . signalr使用系统;使用系统。集合。泛型;使用系统。linq使用系统。线程。任务;命名空间signalrcharsample。hubs { public class groupchathub: hub { public async task addto group(string group name){ await groups。addtogroupasync(上下文。connectionid,group name);等待客户。组(组名)。send async( 发送 ,$ {上下文。connectionid}已加入群{groupname }。 );}公共异步任务removefromgroup(string group name){ await groups。removefromgroupasync(上下文。connectionid,group name);等待客户。组(组名)。send async( 发送 ,$ {上下文。connectionid}已离开群{groupname}。 );}公共任务sendmessagetogroup(string group name,string message) {返回客户端。组(组名)。send async( 发送 、$;{context.connectionid}: {消息} );} public override task onconnectedasync{ return base。onconnectedasync;} }}
标签:
userchathubsendmessage
了解更多宇舶表宇舶表官网(湖北人 s考网-官网)相关内容请关注本站点。