private async Task ReceiveMessagesAsync(WebSocket webSocket, CancellationToken cancellationToken)
        {
            //Loop until a cancel request is issued
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    WebSocketReceiveResult response;
                    //needs optimization, 4096 is in the options, how big are the messages?
                    var buffer  = new byte[4096];
                    var message = new List <byte>();
                    do
                    {
                        //receive entire message and copy it to a list of bytes
                        response = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), cancellationToken);

                        message.AddRange(new ArraySegment <byte>(buffer, 0, response.Count));
                    }while (!response.EndOfMessage);

                    //If the response message is to close, then stop processing
                    if (response.MessageType == WebSocketMessageType.Close)
                    {
                        break;
                    }
                    //assuming it's returning text
                    else if (response.MessageType == WebSocketMessageType.Text)
                    {
                        //Convert to our data model and update the persistence layer
                        var text           = Encoding.UTF8.GetString(message.ToArray());
                        var licenseMessage = JsonConvert.DeserializeObject <LicenseMessage>(text);
                        try
                        {
                            _storageService.UpdateMessageReceived(licenseMessage.Id, licenseMessage.SignedLicenseKey);
                        }
                        catch
                        {
                            // todo: needs refinement
                            //something went wrong, try again later
                            _queueService.Enqueue(licenseMessage);
                            throw;
                        }
                    }
                }
                catch (WebSocketException ex) when(ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
                {
                    //Nothing received, nothing saved, all ok
                }
                catch (OperationCanceledException)
                {
                    // Exit normally
                }
            }
        }
示例#2
0
        public IActionResult SendLicenseRequest([FromBody] LicenseRequestModel licenseRequestModel)
        {
            var licenseDataModel = _mapper.Map <LicenseDataModel>(licenseRequestModel);

            //queque for the websocket to pickup
            _queueService.Enqueue(
                _mapper.Map <LicenseMessage>(licenseDataModel)
                );

            //store for the consumer to get updates
            _storageService.AddOrUpdate(licenseDataModel);

            //201 response (REST)
            var licenseResponse = _mapper.Map <LicenseResponseModel>(licenseDataModel);

            return(CreatedAtAction(nameof(GetLicenseResponse), new { id = licenseResponse.Id }, licenseResponse));
        }