/// <summary> /// 链接webSocket /// </summary> /// <param name="userWebSocket"></param> /// <returns></returns> public async Task SendInitialMessages(CustomWebSocket userWebSocket) { WebSocket webSocket = userWebSocket.WebSocket; var msg = new CustomWebSocketMessage { MessagDateTime = DateTime.Now, Type = WSMessageType.登陆, Text = "登录成功!", Username = "******", Url = "" }; string serialisedMessage = JsonConvert.SerializeObject(msg); byte[] bytes = Encoding.UTF8.GetBytes(serialisedMessage); await webSocket.SendAsync(new ArraySegment <byte>(bytes, 0, bytes.Length), WebSocketMessageType.Text, true, CancellationToken.None); }
/// <summary> /// 处理消息 /// </summary> /// <param name="result"></param> /// <param name="buffer"></param> /// <param name="userWebSocket"></param> /// <param name="wsFactory"></param> /// <returns></returns> public async Task HandleMessage(WebSocketReceiveResult result, byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory, IAutomaticPostingFactory apFactory) { string msg = Encoding.UTF8.GetString(buffer); try { //var message = JsonConvert.DeserializeObject<CustomWebSocketMessage>(msg); String JSON = msg.Replace("\0", ""); CustomWebSocketMessage message = JSON.ConvertToObject <CustomWebSocketMessage>(); if (message.Type == EnumModel.WSMessageType.发送 && message.UserType == UserType.挂工艺数据) { message.Text = HelperAspNet.Http.HttpGet("http://172.16.14.250:7031/MesStationMasterMbjGetApi?StationNo=" + message.Username); string serialisedMessage = JsonConvert.SerializeObject(message); Helperlog4.Info("返回吊挂工艺数据" + serialisedMessage); await BroadcastOthers(Encoding.UTF8.GetBytes(serialisedMessage), message.Username, wsFactory); } else if (message.Type == EnumModel.WSMessageType.发送 && message.UserType == UserType.挂模板数据) { message.Text = HelperAspNet.Http.HttpGet("http://172.16.14.250:7030/MesStationMasterAppGetApi?StationNo=" + message.Username); string serialisedMessage = JsonConvert.SerializeObject(message); Helperlog4.Info("返回吊挂吊挂模板数据" + serialisedMessage); await BroadcastOthers(Encoding.UTF8.GetBytes(serialisedMessage), message.Username, wsFactory); } else if (message.Type == EnumModel.WSMessageType.发送 && message.UserType == UserType.过账客户端) { message.Text = apFactory.AutomaticPostingAll().ConvertToJson(); string serialisedMessage = JsonConvert.SerializeObject(message); Helperlog4.Info("发送过账客户端" + serialisedMessage); await BroadcastOthers(Encoding.UTF8.GetBytes(serialisedMessage), message.Username, wsFactory); //await BroadcastUserTypeOthers(Encoding.UTF8.GetBytes(serialisedMessage), UserType.过账客户端, wsFactory); } else if (message.Type == EnumModel.WSMessageType.发送 && message.UserType == UserType.过账数据端) { if (message.Text == "Update") { message.Text = HelperAspNet.Http.HttpGet("http://172.16.1.34:7777/api/GetAutomaticPosting/GetAutomaticPostingAPI"); List <AutomaticPosting> AutomaticPostingList = new List <AutomaticPosting>(); AutomaticPostingList = message.Text.ConvertToList <AutomaticPosting>(); foreach (AutomaticPosting item in AutomaticPostingList) { apFactory.AutomaticPostingAdd(item); } Helperlog4.Info("添加过账数据端" + AutomaticPostingList.ConvertToJson()); } else if (message.Text == "Get") { List <AutomaticPosting> AutomaticPostingList = new List <AutomaticPosting>(); AutomaticPostingList = apFactory.AutomaticPostingAll(); message.Text = AutomaticPostingList.ConvertToJson(); string serialisedMessage = JsonConvert.SerializeObject(message); await BroadcastUserTypeOthers(Encoding.UTF8.GetBytes(serialisedMessage), message.UserType, wsFactory); Helperlog4.Info("过账数据端Get" + serialisedMessage); } } else if (message.Type == EnumModel.WSMessageType.发送 && message.UserType == UserType.过账数据添加端) { List <AutomaticPosting> AutomaticPostingList = new List <AutomaticPosting>(); AutomaticPostingList = message.Text.ConvertToList <AutomaticPosting>(); foreach (AutomaticPosting item in AutomaticPostingList) { apFactory.AutomaticPostingAdd(item); } string serialisedMessage = JsonConvert.SerializeObject(message); Helperlog4.Info("过账数据添加端" + serialisedMessage); await BroadcastUserTypeOthers(Encoding.UTF8.GetBytes(serialisedMessage), UserType.过账客户端, wsFactory); } else if (message.Type == EnumModel.WSMessageType.发送 && message.UserType == UserType.过账数据删除端) { List <AutomaticPosting> AutomaticPostingList = new List <AutomaticPosting>(); AutomaticPostingList = message.Text.ConvertToList <AutomaticPosting>(); foreach (AutomaticPosting item in AutomaticPostingList) { apFactory.AutomaticPostingRemove(item); Helperlog4.Info("过账数据删除端" + message.Username + " 删除服务器内存数据" + JsonConvert.SerializeObject(item)); } string serialisedMessage = JsonConvert.SerializeObject(message); await BroadcastUserTypeOthers(Encoding.UTF8.GetBytes(serialisedMessage), UserType.过账客户端, wsFactory); Helperlog4.Info("过账数据删除端" + message.Username + "给过账客户端返回数据完成!" + serialisedMessage); } else if (message.Type == WSMessageType.登陆) { await userWebSocket.WebSocket.SendAsync(new ArraySegment <byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); } else if (message.Type == WSMessageType.重连) { // string serialisedMessage = JsonConvert.SerializeObject(message); // await BroadcastOthers(Encoding.UTF8.GetBytes(serialisedMessage), message.Username, wsFactory); } //if (message.Type == WSMessageType.发送 || message.Type == WSMessageType.重连) //{ // string serialisedMessage = JsonConvert.SerializeObject(message); // await BroadcastOthers(Encoding.UTF8.GetBytes(serialisedMessage), message.Username, wsFactory); //} } catch (Exception e) { await userWebSocket.WebSocket.SendAsync(new ArraySegment <byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); } }