Pisces.IM.Client.Web(持续更新...)
Pisces.IM.Client.Web

聊天界面

历史记录
前端主要采用了knockout-3.5.1.js作为数据绑定
websocket作为前后主要通讯手段
需要在Program.cs加上一句启用websocket
1 app.UseWebSockets();
将封装ICommunicationService接口并作为单例模式注入,目的防止刷新导致与通讯服务刷新出现奇怪的bug
Program.cs中
1 builder.Services.AddSingleton<ICommunicationService, CommunicationService>();
在ICommunicationService.cs中
1 public interface ICommunicationService 2 { 3 void SendMessage(ChatContentModel content); 4 5 WebSocket? CurrentClient { get; } 6 7 string MyID { get; } 8 9 void SetCurrentClient(WebSocket client); 10 } 11 12 public class CommunicationService : ICommunicationService 13 { 14 private readonly ICommunication _communication; 15 private readonly IChatContentService _chatContentService; 16 public string MyID { get; } 17 public WebSocket? CurrentClient { get; private set; } 18 19 public CommunicationService(ICommunication communication, IChatContentService chatContentService) 20 { 21 _communication = communication; 22 _communication.ReceiveContentMessage += Communication_ReceiveContentMessage; 23 MyID = _communication.GetID() ?? GenerateIdHelper.GenerateId().ToString(); 24 _chatContentService = chatContentService; 25 26 } 27 28 public async void Communication_ReceiveContentMessage(ContentMessageEventArgs e) 29 { 30 switch (e.MessageType) 31 { 32 case MessageType.Ping: 33 // ReceiveServerPing(e); 34 break; 35 case MessageType.Identity: 36 // GetIdentityMessage(e); 37 break; 38 case MessageType.Content: 39 await GetContentMessage(e); 40 break; 41 case MessageType.File: 42 // GetFileMessage(e); 43 break; 44 case MessageType.Image: 45 break; 46 case MessageType.Video: 47 break; 48 case MessageType.Custom: 49 break; 50 case MessageType.Offline: 51 // ResponseOffline(e); 52 break; 53 default: 54 55 break; 56 57 } 58 } 59 60 private async Task GetContentMessage(ContentMessageEventArgs e) 61 { 62 if (string.IsNullOrEmpty(e.FromId)) return; 63 var fromFriend = _chatContentService.ChatFriendModels 64 .Where(x => x.FriendId == e.FromId).FirstOrDefault(); 65 if (fromFriend == null) return; 66 67 var ChatContent = new ChatContentModel() 68 { 69 FriendId = fromFriend.FriendId, 70 Avatar = fromFriend.Avatar, 71 IsSender = false, 72 Message = e.Content, 73 MessageDateTime = DateTime.Now, 74 NickName = fromFriend.NickName 75 }; 76 77 var json = Newtonsoft.Json.JsonConvert.SerializeObject(ChatContent); 78 var buffer = json.GetBytes(); 79 80 if(CurrentClient != null) { 81 82 _chatContentService.ChatContentModels.Add(ChatContent); 83 84 await CurrentClient.SendAsync( 85 new ArraySegment<byte>(buffer, 0, buffer.Length), 86 WebSocketMessageType.Text, 87 true, 88 CancellationToken.None); 89 } 90 } 91 92 public void SendMessage(ChatContentModel content) 93 { 94 var body = MessageService 95 .CreateMessagePayload(MyID, content.FriendId, content.Message ?? "", 2); 96 var buffer = MessageService.Combined(body); 97 98 _communication.GetCurrentSocket()?.Send(buffer); 99 } 100 101 public void SetCurrentClient(WebSocket client) 102 { 103 CurrentClient = client; 104 } 105 }
完整源码:github
浙公网安备 33010602011771号