麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

不用找了,比較全的signalR例子已經(jīng)為你準(zhǔn)備好了.

2019-11-17 02:03:13
字體:
供稿:網(wǎng)友

不用找了,比較全的signalR例子已經(jīng)為你準(zhǔn)備好了.

這幾天想著將一個(gè)winform的工具上線到web上,因?yàn)閷?duì)時(shí)時(shí)性的要求比較高,找朋友咨詢了一下推薦了SignlarR 框架,比較強(qiáng)大.昨天才看到,今天研究了一下將里面的例子都拿出來共享.

官方的參考:http://www.asp.net/signalr/overview/getting-started

安裝SignalR:NuGet命令:

PM> Install-Package Microsoft.AspNet.SignalR

<------------1:與他人聊天:------------>

后臺(tái)代碼示例:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using Microsoft.AspNet.SignalR; 6 using Microsoft.AspNet.SignalR.Hubs; 7  8 namespace SignalRChat.Hubs.Data 9 {10     [HubName("ViewDataHub")]11     public class ViewDataHub : Hub12     {13         //this is just called by client and return the value for it .14         public string Hello()15         {16             return "hello";17         }18 19 20 21         //this fucntion will be called by client and the inside function 22         //Clients.Others.talk(message);23         //will be called by clinet javascript function .24         public void SendMessag(string message)25         {26             Clients.Others.talk(message);27         }28 29     }30 }
View Code

小提示:注意其它的紅色字體部分

前臺(tái)代碼示例:

 1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <title></title> 5     <!--Script references. --> 6     <!--Reference the jQuery library. --> 7     <script src="Scripts/jquery-1.10.2.min.js"></script> 8     <!--Reference the SignalR library. --> 9     <script src="Scripts/jquery.signalR-2.0.2.js"></script>10     <!--Reference the autogenerated SignalR hub script. -->11     <script src='signalr/hubs'></script>12     <!--Add script to update the page and send messages.-->13     <script type='text/Javascript'>14 15 16         $(function () {17             // Declare a PRoxy to reference the hub.18             var chat = $.connection.ViewDataHub;19 20 21             //init the client function 22             init(chat);23 24 25             $("#btnclick").click(function () {26                 //Response the information27                 $.connection.hub.start().done(function () {28                     chat.server.hello().done(function (res) {29                         alert(res);30                     })//end of done function31                 })//the end of the $.connection32             })//end of click function33 34 35 36             $("#btntalk").click(function () {37                 $.connection.hub.start().done(function () {38                     chat.server.sendMessag($("#txttalk").val());39                     $("#txttalk").val("");40                 })//the end of the $.connection41 42             });//btntalk end43 44         })45 46 47         //init the client method48         function init(chat) {49 50             chat.client.talk = function (message) {51                 var talk = "<h1>" + message + "</h1>";52 53                 $("#dvtalk").append(talk);54 55             } //end regist the client function56 57         } //end of the initfunction58 59     </script>60 </head>61 <body>62     <div>63         <table id="tbtoday"></table>64         <input type="text" id="txttalk" width="150"/>65         <input type="button" id="btnclick" value="clickme" />66         <input type="button" id="btntalk" value="talkwithme" />67         <div id="dvtalk">68 69         </div>70     </div>71 </body>72 </html>
View Code

出現(xiàn)的效果:

兩個(gè)窗口之間的聊天

我知道你心中肯定有疑問,我也是這樣,當(dāng)我剛接觸的時(shí)候完全搞不懂這是為什么會(huì)這樣,我們來回顧一次正常的聊天過程:

那我們重新拆分以上的方法來證明我們的猜想是否正確

1          $("#btntalk").click(function () {2                 $.connection.hub.start().done(function () {3                     chat.server.sendMessag($("#txttalk").val());4                     $("#txttalk").val("");5                 })//the end of the $.connection6 7             });//btntalk end

chat.server.sendMessage(message) 從客戶端調(diào)用了服務(wù)器的方法(服務(wù)器扮演的是中轉(zhuǎn)站的角色).

此時(shí)的message 從客戶端A發(fā)送給了服務(wù)端

那服務(wù)器就應(yīng)該有這樣的一個(gè)方法與之相對(duì)應(yīng)

后臺(tái)代碼:

1    //this fucntion will be called by client and the inside function 2         //Clients.Others.talk(message);3         //will be called by clinet javascript function .4         public void SendMessag(string message)5         {6             Clients.Others.talk(message);7         }

服務(wù)端接收到A發(fā)送來的message.

這個(gè)時(shí)候服務(wù)端將消息推送給客戶端B

Clients.Others.talk(message);

這個(gè)時(shí)候客戶端B應(yīng)該有一個(gè)talk的方法來將消息顯示出來

 1   //init the client method 2         function init(chat) { 3  4             chat.client.talk = function (message) { 5                 var talk = "<h1>" + message + "</h1>"; 6  7                 $("#dvtalk").append(talk); 8  9             } //end regist the client function10 11         } //end of the initfunction

這個(gè)時(shí)候客戶端B接收到消息,用Js的方法顯示出來消息. 一次通話就完成了.

<------------二,客戶端傳遞參數(shù)給服務(wù)端并從服務(wù)端得到返回值:------------>

前端代碼:

 1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <title></title> 5     <!--Script references. --> 6     <!--Reference the jQuery library. --> 7     <script src="Scripts/jquery-1.10.2.min.js"></script> 8     <!--Reference the SignalR library. --> 9     <script src="Scripts/jquery.signalR-2.0.2.js"></script>10     <!--Reference the autogenerated SignalR hub script. -->11     <script src='signalr/hubs'></script>12     <!--Add script to update the page and send messages.-->13     <script type='text/javascript'>14 15 16         $(function () {17             // Declare a proxy to reference the hub.18             var chat = $.connection.ViewDataHub;19 20 21             //init the client function 22             init(chat);23 24 25             $("#btnclick").click(function () {26                 //Response the information27                 $.connection.hub.start().done(function () {28                     chat.server.hello($("#txttalk").val()).done(function (res) {29                         var talk = "<h1>" + res + "</h1>";30 31                         $("#dvtalk").append(talk);32                     })//end of done function33                 })//the end of the $.connection34             })//end of click function35 36 37 38             $("#btntalk").click(function () {39                 $.connection.hub.start().done(function () {40                     chat.server.sendMessag($("#txttalk").val());41                     $("#txttalk").val("");42                 })//the end of the $.connection43 44             });//btntal
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费欧美一级视频 | 国产片91| 31freehdxxxx欧美 | 久久精品23| 国产69精品久久久久孕妇黑 | 欧美黄色三级视频 | 极品销魂一区二区三区 | 亚洲四播房 | 精精国产xxxx视频在线播放7 | 黄视频免费在线观看 | 国产一区二区精品在线观看 | 视频一区二区中文字幕 | 久久国产中文 | 在线亚洲免费视频 | hdjapanesemassagehd日本| 美女被免费网站在线软件 | 精品一区二区久久久久 | 久久精品亚洲成在人线av网址 | 在线小视频国产 | 亚洲午夜网站 | 欧美性生话视频 | 免费在线看黄 | 天堂成人国产精品一区 | 久久99精品久久久久久青青日本 | 精品一区二区三区中文字幕老牛 | www.9191.com| 国产剧情在线观看一区二区 | 久久久久久久久久久一区 | 国产精品自在线拍 | 国产精品自在线拍 | 日本黄色不卡视频 | 久久久成人动漫 | 久久伊人精品视频 | 毛片大全在线观看 | 久久久久久亚洲国产精品 | 国产一区二区三区四区五区在线 | 一级做受毛片免费大片 | 国产精品片一区二区三区 | 日本看片一区二区三区高清 | 成人毛片100部 | 久久久视频免费观看 |