国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - ASP.NET MVC4異步聊天室的示例代碼

ASP.NET MVC4異步聊天室的示例代碼

2020-05-17 14:50逆世風(fēng)靈 ASP.NET教程

這篇文章主要介紹了ASP.NET MVC4異步聊天室的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文介紹了ASP.NET MVC4異步聊天室的示例代碼,分享給大家,具體如下:

類圖:

ASP.NET MVC4異步聊天室的示例代碼

Domain層

IChatRoom.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
 
namespace MvcAsyncChat.Domain
{
  public interface IChatRoom
  {
    void AddMessage(string message);
    void AddParticipant(string name);
    void GetMessages(
      DateTime since,
      Action<IEnumerable<string>, DateTime> callback);
    void RemoveParticipant(string name);
  }
}

IMessageRepo.cs

?
1
2
3
4
5
6
7
8
9
10
11
using System;
using System.Collections.Generic;
 
namespace MvcAsyncChat.Domain
{
  public interface IMessageRepo
  {
    DateTime Add(string message);
    IEnumerable<string> GetSince(DateTime since);
  }
}

ICallbackQueue.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
 
namespace MvcAsyncChat.Domain
{
  public interface ICallbackQueue
  {
    void Enqueue(Action<IEnumerable<string>, DateTime> callback);
    IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueAll();
    IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueExpired(DateTime expiry);
  }
}

ChatRoom.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using MvcAsyncChat.Svcs;
 
namespace MvcAsyncChat.Domain
{
  public class ChatRoom : IChatRoom
  {
    readonly ICallbackQueue callbackQueue;
    readonly IDateTimeSvc dateTimeSvc;
    readonly IMessageRepo messageRepo;
 
    public ChatRoom(
      ICallbackQueue callbackQueue,
      IDateTimeSvc dateTimeSvc,
      IMessageRepo messageRepo)
    {
      this.callbackQueue = callbackQueue;
      this.dateTimeSvc = dateTimeSvc;
      this.messageRepo = messageRepo;
    }
 
    public void AddMessage(string message)
    {
      var timestamp = messageRepo.Add(message);
 
      foreach (var callback in callbackQueue.DequeueAll())
        callback(new[] { message }, timestamp);
    }
 
    public void AddParticipant(string name)
    {
      AddMessage(string.Format("{0} 已進(jìn)入房間.", name));
    }
 
    public void GetMessages(
      DateTime since,
      Action<IEnumerable<string>, DateTime> callback)
    {
      var messages = messageRepo.GetSince(since);
 
      if (messages.Count() > 0)
        callback(messages, since);
      else
        callbackQueue.Enqueue(callback);
    }
 
    public void RemoveParticipant(string name)
    {
      AddMessage(string.Format("{0} left the room.", name));
    }
  }
}

InMemMessageRepo.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace MvcAsyncChat.Domain
{
  public class InMemMessageRepo : IMessageRepo
  {
    public InMemMessageRepo()
    {
      Messages = new List<Tuple<string, DateTime>>();
    }
 
    public IList<Tuple<string, DateTime>> Messages { get; private set; }
 
    public DateTime Add(string message)
    {
      var timestamp = DateTime.UtcNow;
 
      Messages.Add(new Tuple<string, DateTime>(message, timestamp));
 
      return timestamp;
    }
 
    public IEnumerable<string> GetSince(DateTime since)
    {
      return Messages
        .Where(x => x.Item2 > since)
        .Select(x => x.Item1);
    }
  }
}

CallbackQueue.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace MvcAsyncChat.Domain
{
  public class CallbackQueue : ICallbackQueue
  {
    public CallbackQueue()
    {
      Callbacks = new Queue<Tuple<Action<IEnumerable<string>, DateTime>, DateTime>>();
    }
 
    public Queue<Tuple<Action<IEnumerable<string>, DateTime>, DateTime>> Callbacks { get; private set; }
 
    public void Enqueue(Action<IEnumerable<string>, DateTime> callback)
    {
      Callbacks.Enqueue(new Tuple<Action<IEnumerable<string>, DateTime>, DateTime>(callback, DateTime.UtcNow));
    }
 
    public IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueAll()
    {
      while (Callbacks.Count > 0)
        yield return Callbacks.Dequeue().Item1;
    }
 
    public IEnumerable<Action<IEnumerable<string>, DateTime>> DequeueExpired(DateTime expiry)
    {
      if (Callbacks.Count == 0)
        yield break;
 
      var oldest = Callbacks.Peek();
      while (Callbacks.Count > 0 && oldest.Item2 <= expiry)
      {
        yield return Callbacks.Dequeue().Item1;
 
        if (Callbacks.Count > 0)
          oldest = Callbacks.Peek();
      }
    }
  }
}

RequestModels文件夾實體類

EnterRequest.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
 
namespace MvcAsyncChat.RequestModels
{
  public class EnterRequest
  {
    [DisplayName("名稱")]
    [Required, StringLength(16), RegularExpression(@"^[A-Za-z0-9_\ -]+$", ErrorMessage="A name must be alpha-numeric.")]
    public string Name { get; set; }
  }
}

GetMessagesRequest.cs

?
1
2
3
4
5
6
7
8
9
using System;
 
namespace MvcAsyncChat.RequestModels
{
  public class GetMessagesRequest
  {
    public string since { get; set; }
  }
}

SayRequest.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
 
namespace MvcAsyncChat.RequestModels
{
  public class SayRequest
  {
    [Required, StringLength(1024), DataType(DataType.MultilineText)]
    public string Text { get; set; }
  }
}

ResponseModels文件夾實體類

GetMessagesResponse.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
 
namespace MvcAsyncChat.ResponseModels
{
  public class GetMessagesResponse
  {
    public string error { get; set; }
    public IEnumerable<string> messages { get; set; }
    public string since { get; set; }
  }
}

SayResponse.cs

?
1
2
3
4
5
6
7
8
9
using System;
 
namespace MvcAsyncChat.ResponseModels
{
  public class SayResponse
  {
    public string error { get; set; }
  }
}

ChatController.cs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Async;
using MvcAsyncChat.Domain;
using MvcAsyncChat.RequestModels;
using MvcAsyncChat.ResponseModels;
using MvcAsyncChat.Svcs;
 
namespace MvcAsyncChat.Controllers
{
  public class ChatController : AsyncController
  {
    readonly IAuthSvc authSvc;
    readonly IChatRoom chatRoom;
    readonly IDateTimeSvc dateTimeSvc;
 
    public ChatController(
      IAuthSvc authSvc,
      IChatRoom chatRoom,
      IDateTimeSvc dateTimeSvc)
    {
      this.authSvc = authSvc;
      this.chatRoom = chatRoom;
      this.dateTimeSvc = dateTimeSvc;
    }
 
    [ActionName("enter"), HttpGet]
    public ActionResult ShowEnterForm()
    {
      if (User.Identity.IsAuthenticated)
        return RedirectToRoute(RouteName.Room);
 
      return View();
    }
 
    [ActionName("enter"), HttpPost]
    public ActionResult EnterRoom(EnterRequest enterRequest)
    {
      if (!ModelState.IsValid)
        return View(enterRequest);
 
      authSvc.Authenticate(enterRequest.Name);
      chatRoom.AddParticipant(enterRequest.Name);
 
      return RedirectToRoute(RouteName.Room);
    }
 
    [ActionName("room"), HttpGet, Authorize]
    public ActionResult ShowRoom()
    {
      return View();
    }
 
    [ActionName("leave"), HttpGet, Authorize]
    public ActionResult LeaveRoom()
    {
      authSvc.Unauthenticate();
      chatRoom.RemoveParticipant(User.Identity.Name);
 
      return RedirectToRoute(RouteName.Enter);
    }
 
    [HttpPost, Authorize]
    public ActionResult Say(SayRequest sayRequest)
    {
      if (!ModelState.IsValid)
        return Json(new SayResponse() { error = "該請求無效." });
 
      chatRoom.AddMessage(User.Identity.Name+" 說:"+sayRequest.Text);
 
      return Json(new SayResponse());
    }
 
    [ActionName("messages"), HttpPost, Authorize]
    public void GetMessagesAsync(GetMessagesRequest getMessagesRequest)
    {
      AsyncManager.OutstandingOperations.Increment();
 
      if (!ModelState.IsValid)
      {
        AsyncManager.Parameters["error"] = "The messages request was invalid.";
        AsyncManager.Parameters["since"] = null;
        AsyncManager.Parameters["messages"] = null;
        AsyncManager.OutstandingOperations.Decrement();
        return;
      }
 
      var since = dateTimeSvc.GetCurrentDateTimeAsUtc();
      if (!string.IsNullOrEmpty(getMessagesRequest.since))
        since = DateTime.Parse(getMessagesRequest.since).ToUniversalTime();
 
      chatRoom.GetMessages(since, (newMessages, timestamp) =>
      {
        AsyncManager.Parameters["error"] = null;
        AsyncManager.Parameters["since"] = timestamp;
        AsyncManager.Parameters["messages"] = newMessages;
        AsyncManager.OutstandingOperations.Decrement();
      });
    }
 
    public ActionResult GetMessagesCompleted(
      string error,
      DateTime? since,
      IEnumerable<string> messages)
    {
      if (!string.IsNullOrWhiteSpace(error))
        return Json(new GetMessagesResponse() { error = error });
 
      var data = new GetMessagesResponse();
      data.since = since.Value.ToString("o");
      data.messages = messages;
 
      return Json(data);
    }
  }
}

room.js

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var since = "",
  errorCount = 0,
  MAX_ERRORS = 6;
 
function addMessage(message, type) {
  $("#messagesSection > td").append("<div class='" + (type || "") + "'>" + message + "</div>")
}
 
function showError(error) {
  addMessage(error.toString(), "error");
}
 
function onSayFailed(XMLHttpRequest, textStatus, errorThrown) {
  showError("An unanticipated error occured during the say request: " + textStatus + "; " + errorThrown);
}
 
function onSay(data) {
  if (data.error) {
    showError("An error occurred while trying to say your message: " + data.error);
    return;
  }
}
 
function setSayHandler() {
  $("#Text").keypress(function (e) {
    if (e.keyCode == 13) {
      $("#sayForm").submit();
      $("#Text").val("");
      return false;
    }
  });
}
 
function retryGetMessages() {
  if (++errorCount > MAX_ERRORS) {
    showError("There have been too many errors. Please leave the chat room and re-enter.");
  }
  else {
    setTimeout(function () {
      getMessages();
    }, Math.pow(2, errorCount) * 1000);
  }
}
 
function onMessagesFailed(XMLHttpRequest, textStatus, errorThrown) {
  showError("An unanticipated error occured during the messages request: " + textStatus + "; " + errorThrown);
  retryGetMessages();
}
 
function onMessages(data, textStatus, XMLHttpRequest) {
  if (data.error) {
    showError("An error occurred while trying to get messages: " + data.error);
    retryGetMessages();
    return;
  }
 
  errorCount = 0;
  since = data.since;
 
  for (var n = 0; n < data.messages.length; n++)
    addMessage(data.messages[n]);
 
  setTimeout(function () {
    getMessages();
  }, 0);
}
 
function getMessages() {
  $.ajax({
    cache: false,
    type: "POST",
    dataType: "json",
    url: "/messages",
    data: { since: since },
    error: onMessagesFailed,
    success: onMessages,
    timeout: 100000
  });
}

Chat視圖文件夾

Enter.cshtml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@model MvcAsyncChat.RequestModels.EnterRequest
 
@{
  View.Title = "Enter";
  Layout = "~/Views/Shared/_Layout.cshtml";
}
 
@section Head {}
 
<tr id="enterSection">
  <td>
    <h2>[MVC聊天]是使用ASP.NET MVC 3的異步聊天室
    <table>
      <tr>
        <td class="form-container">
          <fieldset>
            <legend>進(jìn)入聊天室</legend>
            @using(Html.BeginForm()) {
              @Html.EditorForModel()
              <input type="submit" value="Enter" />
            }
          </fieldset>
        </td>
      </tr>
    </table>
  </td>
</tr>
 
@section PostScript {
  <script>
    $(document).ready(function() {
      $("#Name").focus(); 
    });
  </script>
}

Room.cshtml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@using MvcAsyncChat;
@using MvcAsyncChat.RequestModels;
@model SayRequest
 
@{
  View.Title = "Room";
  Layout = "~/Views/Shared/_Layout.cshtml";
}
 
@section Head {
  <script src="@Url.Content("~/Scripts/room.js")"></script>
}
 
<tr id="messagesSection">
  <td></td>
</tr>
<tr id="actionsSection">
  <td>
 
    <label for="actionsList">操作:</label>
    <ul id="actionsList">
      <li>@Html.RouteLink("離開房間", RouteName.Leave)</li>
    </ul>
    @using (Ajax.BeginForm("say", new { }, new AjaxOptions() {
      OnFailure = "onSayFailed",
      OnSuccess = "onSay",
      HttpMethod = "POST", }, new { id = "sayForm"})) {
      @Html.EditorForModel()
    }
  </td>
</tr>
 
@section PostScript {
  <script>
    $(document).ready(function() {
      $("#Text").attr("placeholder", "你說:");
      $("#Text").focus();
      setSayHandler();
      getMessages();
    });
  </script>
}

運行結(jié)果如圖:

ASP.NET MVC4異步聊天室的示例代碼

ASP.NET MVC4異步聊天室的示例代碼

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/wulex/article/details/78169106?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 久久久久久精 | 精品欧美一区二区三区久久久 | 日韩欧美中文字幕在线视频 | 一本久久a久久精品亚洲 | 日韩视频精品在线 | 高清国产一区二区三区 | 伊人天天| 国产欧美视频在线 | 久久人人爽爽爽人久久久 | av一区二区在线观看 | 奇米成人| 色吧av | 亚洲在线 | 亚洲精品视频在线播放 | 男人久久天堂 | 黄色成人在线 | 国产一区a | 国产精品视频播放 | 国产美女网站视频 | 欧美久久久久久久 | 在线一区观看 | 国产精品久久久久久久久久免费 | 日韩av在线免费 | 亚洲国产成人精品女人久久 | 在线观看一区二区视频 | 国产激情在线 | 亚洲精品视频在线看 | 日本激情综合网 | 亚洲午夜久久 | 日本成人三级 | 日韩av电影在线观看 | 亚洲国产欧美日韩 | 亚洲天堂一区二区 | 亚洲a网| 国产依人| 自拍偷拍在线视频 | 国产露脸国语对白在线 | 亚洲国产中文字幕在线观看 | 国产精品成av人在线视午夜片 | 午夜爱爱毛片xxxx视频免费看 | 91欧美激情一区二区三区成人 |