Ejemplo n.º 1
0
        static void updateClientSubscribedReaderList(IWebSocketConnection client, List <SubscriberClient> list, List <string> _readerList)
        {
            SubscriberClient c = list.Find((_client) =>
            {
                return(_client.client.ConnectionInfo.Id == client.ConnectionInfo.Id);
            });

            if (c != null)
            {
                c.subscribedReaderList.AddRange(_readerList);
            }
        }
Ejemplo n.º 2
0
        static void addClient(IWebSocketConnection client, List <SubscriberClient> list)
        {
            SubscriberClient c = list.Find((_client) =>
            {
                return(_client.client.ConnectionInfo.Origin == client.ConnectionInfo.Origin);
            });

            if (c == null)
            {
                list.Add(new SubscriberClient(client));
                //Debug.WriteLine("Client ++  => " + list.Count.ToString());
            }
        }
Ejemplo n.º 3
0
        static void removeClient(IWebSocketConnection client, List <SubscriberClient> list)
        {
            SubscriberClient c = list.Find((_client) =>
            {
                return(_client.client.ConnectionInfo.Id == client.ConnectionInfo.Id);
            });

            if (c != null)
            {
                list.Remove(c);
                //Debug.WriteLine("Client --  => " + list.Count.ToString());
            }
        }
Ejemplo n.º 4
0
        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);
                };
            });
        }