不用找了,比较全的signalR例子已经为你准备好了.

网友投稿 465 2022-05-30

这几天想着将一个winform的工具上线到web上,因为对时时性的要求比较高,找朋友咨询了一下推荐了SignlarR 框架,比较强大.昨天才看到,今天研究了一下将里面的例子都拿出来共享.

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

安装SignalR: NuGet命令:

PM> Install-Package Microsoft.AspNet.SignalR

<————1:与他人聊天:————>

后台代码示例:

小提示:注意其它的红色字体部分

前台代码示例:

1 2 3 4 title> 5 <!--</span><span style="color: #008000;">Script references. </span><span style="color: #008000;">--> 6 <!--</span><span style="color: #008000;">Reference the jQuery library. </span><span style="color: #008000;">--> 7 <script src="Scripts/jquery-1.10.2.min.js">script> 8 <!--</span><span style="color: #008000;">Reference the SignalR library. </span><span style="color: #008000;">--> 9 <script src="Scripts/jquery.signalR-2.0.2.js">script> 10 <!--</span><span style="color: #008000;">Reference the autogenerated SignalR hub script. </span><span style="color: #008000;">--> 11 <script src='signalr/hubs'>script> 12 <!--</span><span style="color: #008000;">Add script to update the page and send messages.</span><span style="color: #008000;">--> 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 information 27 $.connection.hub.start().done(function () { 28 chat.server.hello().done(function (res) { 29 alert(res); 30 })//end of done function 31 })//the end of the $.connection 32 })//end of click function 33 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 $.connection 41 42 });//btntalk end 43 44 }) 45 46 47 //init the client method 48 function init(chat) { 49 50 chat.client.talk = function (message) { 51 var talk =" + message + "</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>23</p><p>24</p><p>25</p><p>26</p><p>27</p><p>28</p><p>29</p><p>30</p><p>31</p><p>32</p><p>33</p><p>34</p><p>35</p><p>36</p><p>37</p><p>38</p><p>39</p><p>40</p><p>41</p><p>42</p><p>43</p><p>44</p><p>45</p><p>46</p><p>47</p><p>48</p><p>49</p><p>50</p><p>51</p><p>1 <!DOCTYPE html></p><p>2 <html xmlns="http://www.w3.org/1999/xhtml"></p><p>3 <head></p><p>4     <title>title></p><p>5     <!--</span><span style="color: #008000;">Script references. </span><span style="color: #008000;">--></p><p>6     <!--</span><span style="color: #008000;">Reference the jQuery library. </span><span style="color: #008000;">--></p><p>7     <script src="Scripts/jquery-1.10.2.min.js">script></p><p>8     <!--</span><span style="color: #008000;">Reference the SignalR library. </span><span style="color: #008000;">--></p><p>9     <script src="Scripts/jquery.signalR-2.0.2.js">script></p><p>10     <!--</span><span style="color: #008000;">Reference the autogenerated SignalR hub script. </span><span style="color: #008000;">--></p><p>11     <script src='signalr/hubs'>script></p><p>12     <!--</span><span style="color: #008000;">Add script to update the page and send messages.</span><span style="color: #008000;">--></p><p>13     <script type='text/javascript'></p><p>14</p><p>15</p><p>16         $(function () {</p><p>17             // Declare a proxy to reference the hub.</p><p>18             var chat = $.connection.ViewDataHub;</p><p>19</p><p>20</p><p>21             //init the client function</p><p>22             init(chat);</p><p>23</p><p>24</p><p>25             $("#btnclick").click(function () {</p><p>26                 //Response the information</p><p>27                 $.connection.hub.start().done(function () {</p><p>28                     chat.server.hello().done(function (res) {</p><p>29                         alert(res);</p><p>30                     })//end of done function</p><p>31                 })//the end of the $.connection</p><p>32             })//end of click function</p><p>33</p><p>34</p><p>35</p><p>36             $("#btntalk").click(function () {</p><p>37                 $.connection.hub.start().done(function () {</p><p>38                     chat.server.sendMessag($("#txttalk").val());</p><p>39                     $("#txttalk").val("");</p><p>40                 })//the end of the $.connection</p><p>41</p><p>42             });//btntalk end</p><p>43</p><p>44         })</p><p>45</p><p>46</p><p>47         //init the client method</p><p>48         function init(chat) {</p><p>49</p><p>50             chat.client.talk = function (message) {</p><p>51                 var talk =" + message + "</p><p>1</p><p>View Code</p><p>出现的效果:</p><p>两个窗口之间的聊天</p><p>我知道你心中肯定有疑问,我也是这样,当我刚接触的时候完全搞不懂这是为什么会这样,我们来回顾一次正常的聊天过程:</p><p>那我们重新拆分以上的方法来证明我们的猜想是否正确</p><p>1 $("#btntalk").click(function () { 2 $.connection.hub.start().done(function () { 3 chat.server.sendMessag($("#txttalk").val()); 4 $("#txttalk").val(""); 5 })//the end of the $.connection 6 7 });//btntalk end</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>1          $("#btntalk").click(function () {</p><p>2                 $.connection.hub.start().done(function () {</p><p>3                     chat.server.sendMessag($("#txttalk").val());</p><p>4                     $("#txttalk").val("");</p><p>5                 })//the end of the $.connection</p><p>6</p><p>7             });//btntalk end</p><p>chat.server.sendMessage(message) 从客户端调用了服务器的方法(服务器扮演的是中转站的角色).</p><p>此时的message 从客户端A发送给了服务端</p><p>那服务器就应该有这样的一个方法与之相对应</p><p>后台代码:</p><p>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 }</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>1    //this fucntion will be called by client and the inside function</p><p>2         //Clients.Others.talk(message);</p><p>3         //will be called by clinet javascript function .</p><p>4         public void SendMessag(string message)</p><p>5         {</p><p>6             Clients.Others.talk(message);</p><p>7         }</p><p>服务端接收到A发送来的message.</p><p>这个时候服务端将消息推送给客户端B</p><p>Clients.Others.talk(message);</p><p>这个时候客户端B应该有一个talk的方法来将消息显示出来</p><p>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 function 10 11 } //end of the initfunction</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>1   //init the client method</p><p>2         function init(chat) {</p><p>3</p><p>4             chat.client.talk = function (message) {</p><p>5                 var talk = "<h1>" + message + "h1>";</p><p>6</p><p>7                 $("#dvtalk").append(talk);</p><p>8</p><p>9             } //end regist the client function</p><p>10</p><p>11         } //end of the initfunction</p><p>这个时候客户端B接收到消息,用Js的方法显示出来消息. 一次通话就完成了.</p><p><————二,客户端传递参数给服务端并从服务端得到返回值:————></p><p>前端代码:</p><p>1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <title>title> 5 <!--</span><span style="color: #008000;">Script references. </span><span style="color: #008000;">--> 6 <!--</span><span style="color: #008000;">Reference the jQuery library. </span><span style="color: #008000;">--> 7 <script src="Scripts/jquery-1.10.2.min.js">script> 8 <!--</span><span style="color: #008000;">Reference the SignalR library. </span><span style="color: #008000;">--> 9 <script src="Scripts/jquery.signalR-2.0.2.js">script> 10 <!--</span><span style="color: #008000;">Reference the autogenerated SignalR hub script. </span><span style="color: #008000;">--> 11 <script src='signalr/hubs'>script> 12 <!--</span><span style="color: #008000;">Add script to update the page and send messages.</span><span style="color: #008000;">--> 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 information 27 $.connection.hub.start().done(function () { 28 chat.server.hello($("#txttalk").val()).done(function (res) { 29 var talk = "</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>23</p><p>24</p><p>25</p><p>26</p><p>27</p><p>28</p><p>29</p><p>1 <!DOCTYPE html></p><p>2 <html xmlns="http://www.w3.org/1999/xhtml"></p><p>3 <head></p><p>4     <title>title></p><p>5     <!--</span><span style="color: #008000;">Script references. </span><span style="color: #008000;">--></p><p>6     <!--</span><span style="color: #008000;">Reference the jQuery library. </span><span style="color: #008000;">--></p><p>7     <script src="Scripts/jquery-1.10.2.min.js">script></p><p>8     <!--</span><span style="color: #008000;">Reference the SignalR library. </span><span style="color: #008000;">--></p><p>9     <script src="Scripts/jquery.signalR-2.0.2.js">script></p><p>10     <!--</span><span style="color: #008000;">Reference the autogenerated SignalR hub script. </span><span style="color: #008000;">--></p><p>11     <script src='signalr/hubs'>script></p><p>12     <!--</span><span style="color: #008000;">Add script to update the page and send messages.</span><span style="color: #008000;">--></p><p>13     <script type='text/javascript'></p><p>14</p><p>15</p><p>16         $(function () {</p><p>17             // Declare a proxy to reference the hub.</p><p>18             var chat = $.connection.ViewDataHub;</p><p>19</p><p>20</p><p>21             //init the client function</p><p>22             init(chat);</p><p>23</p><p>24</p><p>25             $("#btnclick").click(function () {</p><p>26                 //Response the information</p><p>27                 $.connection.hub.start().done(function () {</p><p>28                     chat.server.hello($("#txttalk").val()).done(function (res) {</p><p>29                         var talk = "</p><p>" + res + "</p><p>1</p><p>" + res + "</p><p>"; 30 31 $("#dvtalk").append(talk); 32 })//end of done function 33 })//the end of the $.connection 34 })//end of click function 35 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 $.connection 43 44 });//btntalk end 45 46 }) 47 48 49 //init the client method 50 function init(chat) { 51 52 chat.client.talk = function (message) { 53 var talk = "</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>23</p><p>24</p><p>25</p><p>";</p><p>30</p><p>31                         $("#dvtalk").append(talk);</p><p>32                     })//end of done function</p><p>33                 })//the end of the $.connection</p><p>34             })//end of click function</p><p>35</p><p>36</p><p>37</p><p>38             $("#btntalk").click(function () {</p><p>39                 $.connection.hub.start().done(function () {</p><p>40                     chat.server.sendMessag($("#txttalk").val());</p><p>41                     $("#txttalk").val("");</p><p>42                 })//the end of the $.connection</p><p>43</p><p>44             });//btntalk end</p><p>45</p><p>46         })</p><p>47</p><p>48</p><p>49         //init the client method</p><p>50         function init(chat) {</p><p>51</p><p>52             chat.client.talk = function (message) {</p><p>53                 var talk = "</p><p>" + message + "</p><p>1</p><p>" + message + "</p><p>"; 54 55 $("#dvtalk").append(talk); 56 57 } //end regist the client function 58 59 } //end of the initfunction 60 61 script> 62 head> 63 <body> 64 <div> 65 <table id="tbtoday">table> 66 <input type="text" id="txttalk" width="150"/> 67 <input type="button" id="btnclick" value="clickme" /> 68 <input type="button" id="btntalk" value="talkwithme" /> 69 <div id="dvtalk"> 70 71 div> 72 div> 73 body> 74 html></p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>";</p><p>54</p><p>55                 $("#dvtalk").append(talk);</p><p>56</p><p>57             } //end regist the client function</p><p>58</p><p>59         } //end of the initfunction</p><p>60</p><p>61     script></p><p>62 head></p><p>63 <body></p><p>64     <div></p><p>65         <table id="tbtoday">table></p><p>66         <input type="text" id="txttalk" width="150"/></p><p>67         <input type="button" id="btnclick" value="clickme" /></p><p>68         <input type="button" id="btntalk" value="talkwithme" /></p><p>69         <div id="dvtalk"></p><p>70</p><p>71         div></p><p>72     div></p><p>73 body></p><p>74 html></p><p>View Code</p><p>后端代码:</p><p>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 : Hub 12 { 13 //this is just called by client and return the value for it . 14 public string Hello(string msg) 15 { 16 17 string res = "sorry ,i don't know"; 18 if (msg.Contains("hello")) 19 { 20 res = "hello ,guy"; 21 } 22 if (msg.Contains("how")) 23 { 24 res = "how are you ~"; 25 } 26 if (msg.Contains("love")) 27 { 28 res = "don't fall in love with me ~"; 29 } 30 return res; 31 } 32 33 34 35 //this fucntion will be called by client and the inside function 36 //Clients.Others.talk(message); 37 //will be called by clinet javascript function . 38 public void SendMessag(string message) 39 { 40 Clients.Others.talk(message); 41 } 42 43 } 44 }</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>23</p><p>24</p><p>25</p><p>26</p><p>27</p><p>28</p><p>29</p><p>30</p><p>31</p><p>32</p><p>33</p><p>34</p><p>35</p><p>36</p><p>37</p><p>38</p><p style="text-align:center"><img src="https://www.huoban.com/news/zb_users/cache/ly_autoimg/m/MTg1MjI.jpg" alt="不用找了,比较全的signalR例子已经为你准备好了." title="不用找了,比较全的signalR例子已经为你准备好了." /></p><p>39</p><p>40</p><p>41</p><p>42</p><p>43</p><p>44</p><p>1 using System;</p><p>2 using System.Collections.Generic;</p><p>3 using System.Linq;</p><p>4 using System.Web;</p><p>5 using Microsoft.AspNet.SignalR;</p><p>6 using Microsoft.AspNet.SignalR.Hubs;</p><p>7</p><p>8 namespace SignalRChat.Hubs.Data</p><p>9 {</p><p>10     [HubName("ViewDataHub")]</p><p>11     public class ViewDataHub : Hub</p><p>12     {</p><p>13         //this is just called by client and return the value for it .</p><p>14         public string Hello(string msg)</p><p>15         {</p><p>16</p><p>17             string res = "sorry ,i don't know";</p><p>18             if (msg.Contains("hello"))</p><p>19             {</p><p>20                 res = "hello ,guy";</p><p>21             }</p><p>22             if (msg.Contains("how"))</p><p>23             {</p><p>24                 res = "how are you ~";</p><p>25             }</p><p>26             if (msg.Contains("love"))</p><p>27             {</p><p>28                 res = "don't fall in love with me ~";</p><p>29             }</p><p>30             return res;</p><p>31         }</p><p>32</p><p>33</p><p>34</p><p>35         //this fucntion will be called by client and the inside function</p><p>36         //Clients.Others.talk(message);</p><p>37         //will be called by clinet javascript function .</p><p>38         public void SendMessag(string message)</p><p>39         {</p><p>40             Clients.Others.talk(message);</p><p>41         }</p><p>42</p><p>43     }</p><p>44 }</p><p>View Code</p><p>效果图:</p><p><————三,客户端刷时时的刷新数据————></p><p>前端:</p><p>1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>title> 6 <script src="https://www.huoban.com/news/zb_users/upload/2022/05/20220530210801_75510.done(function () { 20 21 $("#btnloop").click(function () { 22 chat.server.refresh().done(function (data) { 23 AppendTable(data); 24 })//done end 25 }) //btnloop end 26 27 28 $("#btnclient").click(function () { 29 chat.server.RefreshClients(); 30 }); 31 32 33 $("#btnclick").click(function () { 34 Start(); 35 }); 36 37 38 $("#btnstop").click(function () { 39 Stop(); 40 }); 41 42 }) 43 }) //$function 44 45 46 function Start() { 47 if (loopdo == null) { 48 loopdo = setInterval('$("#btnloop").click()', 1000); 49 } 50 51 } 52 53 function Stop() { 54 if (loopdo != null) { 55 clearInterval(loopdo); 56 } 57 } 58 59 function AppendTable(data) { 60 arr.length = 0; 61 arr.push(" OpendoorPriceOpentiem</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>23</p><p>24</p><p>25</p><p>26</p><p>27</p><p>28</p><p>29</p><p>30</p><p>31</p><p>32</p><p>33</p><p>34</p><p>35</p><p>36</p><p>37</p><p>38</p><p>39</p><p>40</p><p>41</p><p>42</p><p>43</p><p>44</p><p>45</p><p>46</p><p>47</p><p>48</p><p>49</p><p>50</p><p>51</p><p>52</p><p>53</p><p>54</p><p>55</p><p>56</p><p>57</p><p>58</p><p>59</p><p>60</p><p>61</p><p>62</p><p>1 <!DOCTYPE html></p><p>2 <html xmlns="http://www.w3.org/1999/xhtml"></p><p>3 <head></p><p>4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></p><p>5     <title>title></p><p>6     <script src="https://www.huoban.com/news/zb_users/upload/2022/05/20220530210801_70782.").click(function () {</p><p>22                     chat.server.refresh().done(function (data) {</p><p>23                         AppendTable(data);</p><p>24                     })//done end</p><p>25                 }) //btnloop  end</p><p>26</p><p>27</p><p>28                 $("#btnclient").click(function () {</p><p>29                     chat.server.RefreshClients();</p><p>30                 });</p><p>31</p><p>32</p><p>33                 $("#btnclick").click(function () {</p><p>34                     Start();</p><p>35                 });</p><p>36</p><p>37</p><p>38                 $("#btnstop").click(function () {</p><p>39                     Stop();</p><p>40                 });</p><p>41</p><p>42             })</p><p>43         }) //$function</p><p>44</p><p>45</p><p>46         function Start() {</p><p>47             if (loopdo == null) {</p><p>48                 loopdo = setInterval('$("#btnloop").click()', 1000);</p><p>49             }</p><p>50</p><p>51         }</p><p>52</p><p>53         function Stop() {</p><p>54             if (loopdo != null) {</p><p>55                 clearInterval(loopdo);</p><p>56             }</p><p>57         }</p><p>58</p><p>59         function AppendTable(data) {</p><p>60             arr.length = 0;</p><p>61             arr.push("</p><p>OpendoorPriceOpentiem</p><p>"); 62 $.each(data, function (i) { 63 arr.push(" "); 64 arr.push(" " + data[i].Opendoor + "</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>");</p><p>62             $.each(data, function (i) {</p><p>63                 arr.push("</p><p>");</p><p>64                 arr.push(" " + data[i].Opendoor + "</p><p>"); 65 arr.push(" " + data[i].Price + "</p><p>1</p><p>2</p><p>");</p><p>65                 arr.push(" " + data[i].Price + "</p><p>"); 66 arr.push(" " + data[i].Opentiem + "</p><p>1</p><p>2</p><p>");</p><p>66                 arr.push(" " + data[i].Opentiem + "</p><p>"); 67 arr.push("</p><p>1</p><p>2</p><p>");</p><p>67                 arr.push("</p><p>"); 68 }); 69 $("#tblist").html(arr.join("")); 70 } 71 72 function Init(chat) { 73 chat.client.myrefresh = function (data) { 74 AppendTable(data); 75 } 76 77 } 78 79 script> 80 head> 81 <body> 82 <input type="button" id="btnclick" value="Start" /> 83 84 <input type="button" id="btnloop" value="View" style="display: none" /> 85 86 <input type="button" id="btnstop" value="Stop" /> 87 88 <input type="button" id="btnclient" value="ClientAll" /> 89 <table id="tblist"> 90 table> 91 body> 92 93 94 95 html></p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>23</p><p>24</p><p>25</p><p>26</p><p>27</p><p>28</p><p>29</p><p>");</p><p>68             });</p><p>69             $("#tblist").html(arr.join(""));</p><p>70         }</p><p>71</p><p>72         function Init(chat) {</p><p>73             chat.client.myrefresh = function (data) {</p><p>74                 AppendTable(data);</p><p>75             }</p><p>76</p><p>77         }</p><p>78</p><p>79     script></p><p>80 head></p><p>81 <body></p><p>82     <input type="button" id="btnclick" value="Start" /></p><p>83</p><p>84     <input type="button" id="btnloop" value="View" style="display: none" /></p><p>85</p><p>86     <input type="button" id="btnstop" value="Stop" /></p><p>87</p><p>88     <input type="button" id="btnclient" value="ClientAll" /></p><p>89     <table id="tblist"></p><p>90     table></p><p>91 body></p><p>92</p><p>93</p><p>94</p><p>95 html></p><p>View Code</p><p>后端:</p><p>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 namespace MyReresh.ViewData 8 { 9 [HubName("viewDataHub")] 10 public class ViewDataHub : Hub 11 { 12 [HubMethodName("refresh")] 13 public List Refresh() 14 { 15 return Stock.GetAll(); 16 } 17 18 [HubMethodName("RefreshClients")] 19 public void RefreshClients() 20 { 21 Clients.All.myrefresh(Stock.GetAll()); 22 } 23 24 25 } 26 27 public class Stock 28 { 29 private string opendoor; 30 31 public string Opendoor 32 { 33 get { return opendoor; } 34 set { opendoor = value; } 35 } 36 37 private double price; 38 39 public double Price 40 { 41 get { return price; } 42 set { price = value; } 43 } 44 45 private DateTime opentiem = System.DateTime.Now; 46 47 public DateTime Opentiem 48 { 49 get { return opentiem; } 50 set { opentiem = value; } 51 } 52 53 54 public static List GetAll() 55 { 56 Random rand = new Random(); 57 List list = new List() 58 { 59 new Stock{Opendoor="Door1",Price=rand.NextDouble()*100}, 60 new Stock{Opendoor="Door2",Price=rand.NextDouble()*100}, 61 new Stock{Opendoor="Door3",Price=rand.NextDouble()*100} 62 }; 63 return list; 64 } 65 66 } 67 }</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p><p>20</p><p>21</p><p>22</p><p>23</p><p>24</p><p>25</p><p>26</p><p>27</p><p>28</p><p>29</p><p>30</p><p>31</p><p>32</p><p>33</p><p>34</p><p>35</p><p>36</p><p>37</p><p>38</p><p>39</p><p>40</p><p>41</p><p>42</p><p>43</p><p>44</p><p>45</p><p>46</p><p>47</p><p>48</p><p>49</p><p>50</p><p>51</p><p>52</p><p>53</p><p>54</p><p>55</p><p>56</p><p>57</p><p>58</p><p>59</p><p>60</p><p>61</p><p>62</p><p>63</p><p>64</p><p>65</p><p>66</p><p>67</p><p>1 using System;</p><p>2 using System.Collections.Generic;</p><p>3 using System.Linq;</p><p>4 using System.Web;</p><p>5 using Microsoft.AspNet.SignalR;</p><p>6 using Microsoft.AspNet.SignalR.Hubs;</p><p>7 namespace MyReresh.ViewData</p><p>8 {</p><p>9     [HubName("viewDataHub")]</p><p>10     public class ViewDataHub : Hub</p><p>11     {</p><p>12         [HubMethodName("refresh")]</p><p>13         public List Refresh()</p><p>14         {</p><p>15             return Stock.GetAll();</p><p>16         }</p><p>17</p><p>18         [HubMethodName("RefreshClients")]</p><p>19         public void RefreshClients()</p><p>20         {</p><p>21             Clients.All.myrefresh(Stock.GetAll());</p><p>22         }</p><p>23</p><p>24</p><p>25     }</p><p>26</p><p>27     public class Stock</p><p>28     {</p><p>29         private string opendoor;</p><p>30</p><p>31         public string Opendoor</p><p>32         {</p><p>33             get { return opendoor; }</p><p>34             set { opendoor = value; }</p><p>35         }</p><p>36</p><p>37         private double price;</p><p>38</p><p>39         public double Price</p><p>40         {</p><p>41             get { return price; }</p><p>42             set { price = value; }</p><p>43         }</p><p>44</p><p>45         private DateTime opentiem = System.DateTime.Now;</p><p>46</p><p>47         public DateTime Opentiem</p><p>48         {</p><p>49             get { return opentiem; }</p><p>50             set { opentiem = value; }</p><p>51         }</p><p>52</p><p>53</p><p>54         public static List GetAll()</p><p>55         {</p><p>56             Random rand = new Random();</p><p>57             List list = new List()</p><p>58             {</p><p>59                 new Stock{Opendoor="Door1",Price=rand.NextDouble()*100},</p><p>60                 new Stock{Opendoor="Door2",Price=rand.NextDouble()*100},</p><p>61                 new Stock{Opendoor="Door3",Price=rand.NextDouble()*100}</p><p>62             };</p><p>63             return list;</p><p>64         }</p><p>65</p><p>66     }</p><p>67 }</p><p>View Code</p><p>注:本次实现的所谓的时时刷新数据和官方提供的Demo有很大的差异,并不是后台代码的角度来刷新,而是从前端的角度来实现的。</p><p>====>点我下载DEMO<====</p><p>web前端</p><p> <strong>版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。</strong> </p></div> <div class="article_footer clear"> <div class="fr tag">标签:<a href="https://www.huoban.com/news/tags-7951.html">不用</a> <a href="https://www.huoban.com/news/tags-897.html">比较</a> </div> <div class="bdsharebuttonbox fl share"> <div class="share-widget fl"> <div class="social-share" data-sites="wechat,weibo, qq, qzone"></div> </div> </div> </div> <!-- 广告位ad4 --> <div class="post-navigation clear"> <div class="post-previous fl"> <span>上一篇:</span><a href="https://www.huoban.com/news/post/18521.html">excel表格记忆输入功能的使用教程</a> </div> <div class="post-next fr"> <span>下一篇:</span><a href="https://www.huoban.com/news/post/18523.html">Oracle获取执行计划的方法(六脉神剑)</a> </div> </div> </div> <div class="related_article"> <div class="box_title clear"> <span><i class="icon fa fa-paper-plane"></i>相关文章</span> </div> <div class="related_list clear"> <article class="fl"> <div class="related_img"><a href="https://www.huoban.com/news/post/75412.html"><img src="https://www.huoban.com/news/zb_users/upload/2022/08/20220819175008_70028.png"></a></div> <div class="related_detail"> <h3><a href="https://www.huoban.com/news/post/75412.html" title="两份比较文件,比较失败,怎么办">两份比较文件,比较失败,怎么办</a></h3> <div class="meta"> <span><i class="fa fa-eye"></i>465</span> <span><i class="fa fa-clock-o"></i>2022-05-30</span> </div> </div> </article> <article class="fl"> <div class="related_img"><a href="https://www.huoban.com/news/post/75374.html"><img src="https://www.huoban.com/news/zb_users/cache/ly_autoimg/n/NzUzNzQ.jpg"></a></div> <div class="related_detail"> <h3><a href="https://www.huoban.com/news/post/75374.html" title="论文查重(论文查重怎么改才能降低重复率)">论文查重(论文查重怎么改才能降低重复率)</a></h3> <div class="meta"> <span><i class="fa fa-eye"></i>465</span> <span><i class="fa fa-clock-o"></i>2022-05-30</span> </div> </div> </article> <article class="fl"> <div class="related_img"><a href="https://www.huoban.com/news/post/74144.html"><img src="https://www.huoban.com/news/zb_users/upload/2022/08/20220814115001_97016.png"></a></div> <div class="related_detail"> <h3><a href="https://www.huoban.com/news/post/74144.html" title="怎样核对两个表的数据(两个表格怎样核对数据)">怎样核对两个表的数据(两个表格怎样核对数据)</a></h3> <div class="meta"> <span><i class="fa fa-eye"></i>465</span> <span><i class="fa fa-clock-o"></i>2022-05-30</span> </div> </div> </article> </div> </div> <!--<p class="comment-disable sb br mb"><i class="iconfont icon-cry"></i>抱歉,评论功能暂时关闭!</p>--> </div> </div> <div class="sidebar"> <div id="推荐文章" class="part clear 推荐文章"> <div class="top"> <h3 class="title">推荐文章</h3> </div> <div class="side 推荐文章"><ul><ul class="hot_posts"> <li><h4><a href="https://www.huoban.com/news/post/132763.html" title="企业生产管理是什么,企业生产管理软件">企业生产管理是什么,企业生产管理软件</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/136160.html" title="盘点进销存软件排行榜前十名">进盘点进销存软件排行榜前十名</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132779.html" title="进销存系统哪个简单好用?进销存系统优点">进销存系统哪个简单好用?进销存系统优点</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/133648.html" title="工厂生产管理(工厂生产管理流程及制度)">工厂生产管理(工厂生产管理流程及制度)</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132780.html" title="生产管理软件,机械制造业生产管理,制造业生产过程管理软件">生产管理软件,机械制造业生产管理,制造业生产过程管理软件</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132776.html" title="进销存软件和ERP有什么区别?进销存与erp软件理解">进销存软件和ERP有什么区别?进销存与erp软件理解</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132974.html" title="进销存如何进行库存管理">进销存如何进行库存管理</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132269.html" title="excel销售订单管理系统(销售订单录入系统)">如何利用excel制作销售订单管理系统?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/136946.html" title="数据库订单管理系统有哪些功能?数据库订单管理系统怎么设计?">数据库订单管理系统有哪些功能?数据库订单管理系统怎么设计?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132312.html" title="数据库订单管理系统(订单系统数据流图)">什么是数据库管理系统?</a></h4></li></ul></ul></div> </div> <div id="divPrevious" class="part clear previous"> <div class="top"> <h3 class="title">最近发表</h3> </div> <div class="side divPrevious"><ul><li><a title="Excel四舍五入设置方法" href="https://www.huoban.com/news/post/154984.html">Excel四舍五入设置方法</a></li> <li><a title="Excel表格乘法计算方法详解" href="https://www.huoban.com/news/post/154983.html">Excel表格乘法计算方法详解</a></li> <li><a title="如何设置Excel行间距以提高阅读清晰度" href="https://www.huoban.com/news/post/154982.html">如何设置Excel行间距以提高阅读清晰度</a></li> <li><a title="Excel怎么画折线图,详细教程带你轻松实现" href="https://www.huoban.com/news/post/154981.html">Excel怎么画折线图,详细教程带你轻松实现</a></li> <li><a title="Excel表格怎么画斜线" href="https://www.huoban.com/news/post/154980.html">Excel表格怎么画斜线</a></li> <li><a title="Excel如何固定表头" href="https://www.huoban.com/news/post/154979.html">Excel如何固定表头</a></li> <li><a title="Excel中如何计算年龄" href="https://www.huoban.com/news/post/154978.html">Excel中如何计算年龄</a></li> <li><a title="要取消Excel密码,这里有几种简单又有效的方法!" href="https://www.huoban.com/news/post/154977.html">要取消Excel密码,这里有几种简单又有效的方法!</a></li> <li><a title="Excel如何设置行高" href="https://www.huoban.com/news/post/154976.html">Excel如何设置行高</a></li> <li><a title="Excel教程,如何使用Excel函数计数进行数据统计" href="https://www.huoban.com/news/post/154975.html">Excel教程,如何使用Excel函数计数进行数据统计</a></li> </ul></div> </div> <div id="sidebar_ad" class="part clear sidebar_ad"> <div class="part sidebar_ad"><div class="active"><a href='https://mrhnug.r.huobanbot.com/wxwork/pub/landings/426?app_id=1960&company_id=54&corp_id=wwbd4b7b6e7b0ccdaa&app_company_id=1' target='_blank'><img style='width:100%;height:100%' src='https://www.huoban.com/news/zb_users/upload/2023/08/erwei3.jpg'></a><br> <a href='https://www.huoban.com/crm.html?utm=jiasouadv' target='_blank'><img style='width:100%;height:100%' src='https://www.huoban.com/news/zb_users/upload/2023/03/20230321171645167939020583758.png'></a><br> </div></div> </div> <div id="hot_posts" class="part clear hot_posts"> <div class="top"> <h3 class="title">热评文章</h3> </div> <ul class="hot_posts"><li><h4><a href="https://www.huoban.com/news/post/104011.html" title="零代码开发是什么?2022低代码平台排行榜">零代码开发是什么?2022低代码平台排行榜</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/131019.html" title="智能进销存库存管理系统(智慧进销存)">智能进销存库存管理系统(智慧进销存)</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/73907.html" title="在线文档哪家强?8款在线文档编辑软件推荐">在线文档哪家强?8款在线文档编辑软件推荐</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/102663.html" title="WPS2016怎么绘制简单的价格表?">WPS2016怎么绘制简单的价格表?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/35776.html" title="什么是在线文档?怎么发在线文档">什么是在线文档?怎么发在线文档</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/151831.html" title="客户管理工具是什么?">客户管理工具是什么?</a></h4></li></ul> </div> <div id="divLinkage" class="part clear link"> <div class="top"> <h3 class="title">友情链接</h3> </div> <div class="side divLinkage"><ul><li class="link-item"><a href="https://www.huoban.com/" target="_blank" title="伙伴云">伙伴云</a></li><li class="link-item"><a href="https://www.huoban.com/news/category-19.html" title="进销存管理">进销存管理</a></li><li class="link-item"><a href="https://www.huoban.com/news/category-3.html" title="低代码">低代码</a></li><li class="link-item"><a href="https://www.huoban.com/news/tags-12.html" target="_blank" title="Excel表格">Excel表格</a></li></ul></div> </div> </div> </div> </section> </div> <footer class="p-footer"> <div class="contant_box"> <div class="discover_tmt"> <h5 class="">伙伴云</h5> <div class="text_box"> <a href="https://jiasou.cn/" title="2B数字化营销SEO">加搜toBSEO</a> <a href="https://www.eulee.cn/article/" title="三维数据引擎">产业元宇宙资讯</a> <a href="https://www.weiling.cn/info/" title="客户营销管理资讯中心">卫瓴CRM资讯</a> <a href="https://www.zkj.com/news/" title="外呼系统新闻资讯中心">外呼系统资讯</a> <a href="https://www.transfertech.cn/news/" title="3D工业相机资讯">3D工业相机资讯</a> <a href="https://www.xinfushe.com/article/" title="灵活用工资讯">灵活用工资讯</a> <a href="http://www.weihusm.com/" title="阿伟常识网">阿伟常识网</a> <a href="http://www.ruishiqiba.com/" title="懂球帝旅游网">懂球帝旅游网</a> </div> </div> <div class="collaboration_box"> </div> <div class="we_img_box clear"> <div class="img_box"> <img src="https://www.huoban.com/news/zb_users/theme/zblog5_news/image/ewm.png" alt="" class="hover_tmt"> </div> </div> </div> <p class="info"> <a href="https://beian.miit.gov.cn" target="_blank" rel="nofollow">京ICP备12038259号</a> <span> <a href="#"></a></span> </p> </footer> <div id="backtop" class="backtop"> <div class="bt-box top"> <i class="fa fa-angle-up fa-2x"></i> </div> </div> <script charset="UTF-8" src="https://www.huoban.com/assets/js/sensorsdata.1.22.2.min.js"></script> <script charset="UTF-8"> var sensors = window['sensorsDataAnalytic201505']; sensors.init({ server_url: 'https://saapi.huoban.com/sa?project=production', heatmap:{scroll_notice_map:'not_collect'}, use_client_time:true, send_type:'beacon' }); sensors.quick('autoTrack'); </script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?6444c045836d6bf27124085a4f62c2a8"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script> (()=>{const e="https://analyze.jiasou.cc/api/v1/page_view/report/",n="9fe06d4884e0461caaa1de5651164d43";let t=null;const o=new Proxy({},{get:(e,n)=>localStorage.getItem(window.btoa(n)),set:(e,n,t)=>!!t&&(localStorage.setItem(window.btoa(n),t),!0)});new Promise((t=>{if(o.fingerprint)t();else{const a=function(){var e={};if(e.userAgent=navigator.userAgent||"",e.plugins=[],navigator.plugins&&navigator.plugins.length>0)for(var n=0;n<navigator.plugins.length;n++){var t={name:navigator.plugins[n].name||"",filename:navigator.plugins[n].filename||"",description:navigator.plugins[n].description||""};e.plugins.push(t)}e.languages=navigator.languages||[navigator.language||""],e.timezone=(new Date).getTimezoneOffset(),e.screenResolution={width:window.screen.width||0,height:window.screen.height||0,pixelDepth:window.screen.pixelDepth||0,colorDepth:window.screen.colorDepth||0};var o=document.createElement("canvas").getContext("2d"),a=[],i=["monospace","sans-serif","serif"];for(n=0;n<i.length;n++){var r=i[n];o.font="12px "+r,o.measureText("abcdefghijklmnopqrstuvwxyz0123456789").width>0&&a.push(r)}return e.fonts=a,e.cookieEnabled=navigator.cookieEnabled||!1,e.localStorage=void 0!==window.localStorage,e.sessionStorage=void 0!==window.sessionStorage,e.doNotTrack="1"===navigator.doNotTrack||"1"===window.doNotTrack||"1"===navigator.msDoNotTrack||"yes"===navigator.doNotTrack,e}();fetch(`${e}u/`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:n,f:window.btoa(JSON.stringify(a))})}).then((e=>{console.debug("browser fingerprint sent"),200===e.status&&e.json().then((e=>{console.debug("browser fingerprint received",e),o.fingerprint=e.fp,t()}))}))}})).then((()=>{e&&o.fingerprint&&fetch(e+`?${new URLSearchParams({token:n}).toString()}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({c:window.btoa(JSON.stringify({u:o.fingerprint,l:window.location.href,r:document.referrer}))})}).then((e=>{200==e.status&&e.json().then((e=>{e.track_id&&(t=e.track_id)}))}))})),window.addEventListener("beforeunload",(async n=>{t&&fetch(e+`?${new URLSearchParams({track_id:t}).toString()}`,{method:"GET",headers:{"Content-Type":"text/plain"},keepalive:!0}),n.returnValue=""}))})(); </script><script language="javascript" src="https://www.huoban.com/news/zb_users/plugin/ZF_ad/js/index.js?id=741"></script> <script language="javascript" src="https://www.huoban.com/news/zb_users/plugin/ZF_ad/js/ZF_ad__cookie.js"></script> </body> </html> <!--123.54 ms , 18 queries , 3675kb memory , 0 error-->