static void ReportEventChangedTag() { List <TagInfo> tags = TagPool.GetAllEventChangedTags(); List <SubscriberClient> list = new List <SubscriberClient>(ClientList); foreach (SubscriberClient socket in list) { List <int> ports = ReaderManager.getPortsByName(socket.subscribedReaderList); List <TagInfo> filtedTags = tags.Where((_ti) => { return(ports.Contains(_ti.port)); }).ToList <TagInfo>(); string json = JsonConvert.SerializeObject(filtedTags); socket.client.Send(json); } }
static void StartWebSocketServer(int _websocketPort) { string url = "ws://localhost:" + _websocketPort.ToString(); WebSocketServer server = new WebSocketServer(url); server.Start(socket => { string originurl = socket.ConnectionInfo.Host + socket.ConnectionInfo.Path; socket.OnOpen = () => { Console.WriteLine(originurl + " connected"); if (socket.ConnectionInfo.Path == "/Client") { addClient(socket, ClientList); Debug.WriteLine("Client ++ => " + ClientList.Count.ToString()); } }; socket.OnClose = () => { Console.WriteLine(originurl + " closed"); removeClient(socket, ClientList); }; socket.OnMessage = message => { Debug.WriteLine("OnMessage => " + message); try { MessageInfo mi = JsonConvert.DeserializeObject <MessageInfo>(message) as MessageInfo; if (mi != null) { List <TagInfo> tags; string json; switch (mi.command) { case MessageInfo.GET_ALL_TAGS: tags = TagPool.GetAllExistsTags(); json = JsonConvert.SerializeObject(tags); socket.Send(json); break; case MessageInfo.GET_SUBSCRIBE_READER_TAGS: SubscriberClient sc = getClientInfo(socket, ClientList); tags = TagPool.GetExistsTags(ReaderManager.getPortsByName(sc.subscribedReaderList)); json = JsonConvert.SerializeObject(tags); socket.Send(json); break; case MessageInfo.SUBSCRIBE_READER: Debug.WriteLine("subscribe => " + mi.content); string[] reader = mi.content.Split(','); if (reader.Length > 0) { updateClientSubscribedReaderList(socket, ClientList, reader.ToList <string>()); } break; } } } catch (Exception e) { Console.WriteLine(e.Message); } /* * [{"port":9601,"bThisTagExists":true,"antennaID":"04","tagType":"","epc":"300833B2DDD906C001010101","antReadCountList":[{"antID":"04","count":0}],"milliSecond":0,"Event":"tagNew"},{"port":9601,"bThisTagExists":true,"antennaID":"02","tagType":"","epc":"300833B2DDD906C001010102","antReadCountList":[{"antID":"02","count":0}],"milliSecond":0,"Event":"tagNew"}] * */ }; socket.OnError = (error) => { Debug.WriteLine("OnError => " + error.Data); removeClient(socket, ClientList); }; }); }