Beispiel #1
0
        private void ReceivedMessage(ArraySegment <byte> buffer)
        {
            var contents = System.Text.Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);

            var envelope = contents.FromJson <WebSocketMessageEnvelope>();

            try
            {
                if (!string.IsNullOrEmpty(envelope.Cid))
                {
                    // Handle message response.
                    TaskCompletionSource <WebSocketMessageEnvelope> completer;
                    var cid = envelope.Cid;
                    _responses.TryRemove(cid, out completer);
                    if (completer == null)
                    {
                        Logger?.ErrorFormat("No completer for message cid: {0}", envelope.Cid);
                        return;
                    }

                    if (envelope.Error != null)
                    {
                        completer.SetException(new WebSocketException(WebSocketError.InvalidState,
                                                                      envelope.Error.Message));
                    }
                    else
                    {
                        completer.SetResult(envelope);
                    }
                }
                else if (envelope.Error != null)
                {
                    ReceivedError?.Invoke(new WebSocketException(WebSocketError.InvalidState, envelope.Error.Message));
                }
                else if (envelope.ChannelMessage != null)
                {
                    ReceivedChannelMessage?.Invoke(envelope.ChannelMessage);
                }
                else if (envelope.ChannelPresenceEvent != null)
                {
                    ReceivedChannelPresence?.Invoke(envelope.ChannelPresenceEvent);
                }
                else if (envelope.MatchmakerMatched != null)
                {
                    ReceivedMatchmakerMatched?.Invoke(envelope.MatchmakerMatched);
                }
                else if (envelope.MatchPresenceEvent != null)
                {
                    ReceivedMatchPresence?.Invoke(envelope.MatchPresenceEvent);
                }
                else if (envelope.MatchState != null)
                {
                    ReceivedMatchState?.Invoke(envelope.MatchState);
                }
                else if (envelope.NotificationList != null)
                {
                    foreach (var notification in envelope.NotificationList.Notifications)
                    {
                        ReceivedNotification?.Invoke(notification);
                    }
                }
                else if (envelope.StatusPresenceEvent != null)
                {
                    ReceivedStatusPresence?.Invoke(envelope.StatusPresenceEvent);
                }
                else if (envelope.StreamPresenceEvent != null)
                {
                    ReceivedStreamPresence?.Invoke(envelope.StreamPresenceEvent);
                }
                else if (envelope.StreamState != null)
                {
                    ReceivedStreamState?.Invoke(envelope.StreamState);
                }
                else
                {
                    Logger?.ErrorFormat("Received unrecognised message: '{0}'", contents);
                }
            }
            catch (Exception e)
            {
                ReceivedError?.Invoke(e);
            }
        }
Beispiel #2
0
        private void ReceivedMessage(ArraySegment <byte> buffer)
        {
            var contents = System.Text.Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);

            Logger?.DebugFormat("Received JSON over web socket: {0}", contents);

            var envelope = contents.FromJson <WebSocketMessageEnvelope>();

            try
            {
                if (!string.IsNullOrEmpty(envelope.Cid))
                {
                    lock (_lockObj)
                    {
                        // Handle message response.
                        if (_responses.ContainsKey(envelope.Cid))
                        {
                            TaskCompletionSource <WebSocketMessageEnvelope> completer = _responses[envelope.Cid];
                            _responses.Remove(envelope.Cid);

                            if (envelope.Error != null)
                            {
                                completer.SetException(new WebSocketException(WebSocketError.InvalidState, envelope.Error.Message));
                            }
                            else
                            {
                                completer.SetResult(envelope);
                            }
                        }
                        else
                        {
                            Logger?.ErrorFormat("No completer for message cid: {0}", envelope.Cid);
                        }
                    }
                }
                else if (envelope.Error != null)
                {
                    ReceivedError?.Invoke(new WebSocketException(WebSocketError.InvalidState, envelope.Error.Message));
                }
                else if (envelope.ChannelMessage != null)
                {
                    ReceivedChannelMessage?.Invoke(envelope.ChannelMessage);
                }
                else if (envelope.ChannelPresenceEvent != null)
                {
                    ReceivedChannelPresence?.Invoke(envelope.ChannelPresenceEvent);
                }
                else if (envelope.MatchmakerMatched != null)
                {
                    ReceivedMatchmakerMatched?.Invoke(envelope.MatchmakerMatched);
                }
                else if (envelope.MatchPresenceEvent != null)
                {
                    ReceivedMatchPresence?.Invoke(envelope.MatchPresenceEvent);
                }
                else if (envelope.MatchState != null)
                {
                    ReceivedMatchState?.Invoke(envelope.MatchState);
                }
                else if (envelope.NotificationList != null)
                {
                    foreach (var notification in envelope.NotificationList.Notifications)
                    {
                        ReceivedNotification?.Invoke(notification);
                    }
                }
                else if (envelope.StatusPresenceEvent != null)
                {
                    ReceivedStatusPresence?.Invoke(envelope.StatusPresenceEvent);
                }
                else if (envelope.StreamPresenceEvent != null)
                {
                    ReceivedStreamPresence?.Invoke(envelope.StreamPresenceEvent);
                }
                else if (envelope.StreamState != null)
                {
                    ReceivedStreamState?.Invoke(envelope.StreamState);
                }
                else if (envelope.Party != null)
                {
                    ReceivedParty?.Invoke(envelope.Party);
                }
                else if (envelope.PartyClose != null)
                {
                    ReceivedPartyClose?.Invoke(envelope.PartyClose);
                }
                else if (envelope.PartyData != null)
                {
                    ReceivedPartyData?.Invoke(envelope.PartyData);
                }
                else if (envelope.PartyJoinRequest != null)
                {
                    ReceivedPartyJoinRequest?.Invoke(envelope.PartyJoinRequest);
                }
                else if (envelope.PartyLeader != null)
                {
                    ReceivedPartyLeader?.Invoke(envelope.PartyLeader);
                }
                else if (envelope.PartyMatchmakerTicket != null)
                {
                    ReceivedPartyMatchmakerTicket?.Invoke(envelope.PartyMatchmakerTicket);
                }
                else if (envelope.PartyPresenceEvent != null)
                {
                    ReceivedPartyPresence?.Invoke(envelope.PartyPresenceEvent);
                }
                else
                {
                    Logger?.ErrorFormat("Received unrecognised message: '{0}'", contents);
                }
            }
            catch (Exception e)
            {
                ReceivedError?.Invoke(e);
            }
        }