本文主要介紹宇舶表宇舶表官網(wǎng)(湖北人 s考網(wǎng)-官網(wǎng)),下面一起看看宇舶表宇舶表官網(wǎng)(湖北人 s考網(wǎng)-官網(wǎng))相關(guān)資訊。
public class str:中心{ public channel reader string readlogstream{ var channel = channel。createunboundedstring;_ = writefileline(通道。作家);返回頻道。讀者;}私有異步任務(wù)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);等待任務(wù)。延遲(時(shí)間跨度。fromseconds(2))。}作家。try complete;} } }使用微軟。aspnetcore . authorization使用微軟。aspnetcore . signalr使用系統(tǒng);使用系統(tǒng)。集合。泛型;使用系統(tǒng)。linq使用系統(tǒng)。線程。任務(wù);命名空間signalrcharsample。hubs {[authorize]public class userchathub: hub { public async task sendmessage(string to username,string message) { await clients。用戶(tousername)。send async( 接收消息 ,$ {日期時(shí)間?,F(xiàn)在}-{上下文。用戶標(biāo)識(shí)符}-{消息} );} }}
!doctype html html head meta charset = utf-8和/ title/title/headbody div用戶名:輸入類型= 文本 id = 用戶輸入 / message:輸入類型= 文本 id = 消息輸入 /input type = 按鈕 id = 發(fā)送按鈕 價(jià)值= 發(fā)送 //div ul id = 信息列表/ ul腳本src = http://localhost: 57634/lib/jquery/dist/jquery . js /script腳本src = http://localhost: 57634/lib/signalr . min . js /script腳本變量連接=新信號(hào)r。hubconnectionbuilder。with url( http://localhost: 57634/chathub ).build;connection . on( 接收消息 ,函數(shù)(用戶,消息){ var msg =用戶 說 消息;var li = document.createelement李 );li.textcontent = msgdocument . getelementbyid( 信息列表).appendchild(李);});connection.start。catch(function(err){ return console . error(err . tostring);});document . getelementbyid( 發(fā)送按鈕 ).addevent listener( 點(diǎn)擊 ,function(event){ var user = document . getelementbyid( 用戶輸入 ).價(jià)值;var消息=document . getelementbyid( 消息輸入 ).價(jià)值;connection . invoke(發(fā)送消息 用戶、消息)。catch(function(err){ return console . error(err . tostring);});event . prevent default;});/script/body/html
使用微軟。aspnetcore . signalr使用系統(tǒng);使用系統(tǒng)。集合。泛型;使用系統(tǒng)。linq使用系統(tǒng)。線程。任務(wù);命名空間signalrcharsample。hubs { public class chathub : hub { public async task sendmessage(string user,string message) { await clients。send async( 接收消息 ,用戶,日期時(shí)間。現(xiàn)在 - 消息);}公共任務(wù)sendmessagetocaller(字符串消息){返回客戶端。send async( 接收消息 ,消息);}公共任務(wù)sendmessagetogroups(string message){ liststring groups = new liststring{ 第一組 , 第二組 };回頭客。團(tuán)體(團(tuán)體)。send async( 接收消息 ,消息);} public override async task onconnectedasync{ await groups。addtogroupasync(上下文。connection id 第一組 );等待基地。onconnectedasync;}公共o在disconnectedasync上覆蓋異步任務(wù)(異常異常){ await groups。removefromgroupasync(上下文。connection id 第一組 );等待基地。ondisconnectedasync(異常);} } }使用微軟。aspnetcore . signalr使用系統(tǒng);使用系統(tǒng)。集合。泛型;使用系統(tǒng)。linq使用系統(tǒng)。線程。任務(wù);命名空間signalrcharsample。hubs { public class custom us: iuseridprovider { public string get userid(hubconnectioncontext connection){//返回連接。gethttpcontextrequest . query[ 用戶 ];回路連接。用戶?。findfirst( 用戶名和密碼)?。價(jià)值;} } }使用微軟。aspnetcore . signalr使用系統(tǒng);使用系統(tǒng)。集合。泛型;使用系統(tǒng)。linq使用系統(tǒng)。線程。任務(wù);命名空間signalrcharsample。hubs { public class groupchathub: hub { public async task addto group(string group name){ await groups。addtogroupasync(上下文。connectionid,group name);等待客戶。組(組名)。send async( 發(fā)送 ,$ {上下文。connectionid}已加入群{groupname }。 );}公共異步任務(wù)removefromgroup(string group name){ await groups。removefromgroupasync(上下文。connectionid,group name);等待客戶。組(組名)。send async( 發(fā)送 ,$ {上下文。connectionid}已離開群{groupname}。 );}公共任務(wù)sendmessagetogroup(string group name,string message) {返回客戶端。組(組名)。send async( 發(fā)送 、$;{context.connectionid}: {消息} );} public override task onconnectedasync{ return base。onconnectedasync;} }}
標(biāo)簽:
userchathubsendmessage
了解更多宇舶表宇舶表官網(wǎng)(湖北人 s考網(wǎng)-官網(wǎng))相關(guān)內(nèi)容請(qǐng)關(guān)注本站點(diǎn)。