/// <summary> /// 加入group 連線 /// </summary> /// <param name="key">groupName</param> /// <param name="connectionId">連線者代碼</param> /// <param name="version">欲使用的版號,可NULL[代表最舊的版號]</param> public void Add(T key, string connectionId, int version) { lock (_groups) { var oldGroup = new SignalRGroupModel(); var newGroup = new SignalRGroupModel(); var connectionList = new List <SignalRConnectionModel>(); lock (connectionList) { var addConnectionModel = new SignalRConnectionModel() { Connection = connectionId, Version = version }; oldGroup = _groups.Where(t => t.GroupName == key.ToString().ToLower()).FirstOrDefault(); newGroup = _groups.Where(t => t.GroupName == key.ToString().ToLower()).FirstOrDefault(); if (oldGroup == null) { newGroup = new SignalRGroupModel(); newGroup.ConnectionList = new List <SignalRConnectionModel>(); newGroup.Connections = new List <string>(); newGroup.GroupName = key.ToString().ToLower(); newGroup.Versions = new List <int>(); } if (newGroup.ConnectionList.FirstOrDefault(t => t.Connection == connectionId) == null) { newGroup.ConnectionList.Add(addConnectionModel); } if (newGroup.Connections.FirstOrDefault(t => t == connectionId) == null) { newGroup.Connections.Add(connectionId); } if (newGroup.Versions.Count() == 0) { newGroup.Versions.Add(version); } else if (!newGroup.Versions.Where(t => t == version).Any()) { newGroup.Versions.Add(version); } //再塞入新的資訊 _groups.Add(newGroup); _groups.Remove(oldGroup); } } }
/// <summary> /// 處理連線列表 - 組成一組相同版號的connecitons列表 /// </summary> public static List <string> ConnectionsProcess(SignalRGroupModel groups, int connectionVersion) { var result = new List <string>(); //查出version的connections var tempConnections = groups.ConnectionList.Where(t => t.Version == connectionVersion); //根據版號塞人 foreach (var conneciton in tempConnections) { result.Add(conneciton.Connection); } return(result); }