Esempio n. 1
0
 private async Task SendDataToSocket <T>(string methodName, HubSocket socket, T data)
 {
     var hubData = new HubData()
     {
         Data       = data,
         MethodName = methodName,
         PromiseId  = -1
     };
     await socket.SendData(hubData);
 }
        public async Task AddSocket(WebSocket webSocket)
        {
            using var hubSocket = new HubSocket(Guid.NewGuid(), webSocket);

            hubSocketRepository.AddOrUpdate(hubSocket.ID, hubSocket);
            hubSocket.DataReceived += DataReceived;

            DataReceived(this, new HubSocketEventArgs()
            {
                Data = JsonConvert.SerializeObject(new HubData()
                {
                    MethodName = nameof(Hub.OnConnectedAsync)
                }),
                HubSocket = hubSocket
            });

            HubData hubSocketConnected = new HubData()
            {
                Data       = hubSocket.ID,
                MethodName = "HubSocketConnected"
            };
            await hubSocket.SendData(hubSocketConnected);

            await hubSocket.ListenLoop(); // infinite loop until socket closed.

            RemoveSocket(hubSocket.ID);

            DataReceived(this, new HubSocketEventArgs()
            {
                Data = JsonConvert.SerializeObject(new HubData()
                {
                    MethodName = nameof(Hub.OnDisconnectedAsync)
                }),
                HubSocket = hubSocket
            });
        }