public WebSocketEventListener(WebSocketOptions options)
        {
            options.ValidateOptions();
            _options = options.Clone();

            var opts = new WebSocketListenerOptions
            {
                // Must disable negotiation timeout for AOT iOS support.
                NegotiationTimeout           = TimeSpan.Zero,
                PingTimeout                  = TimeSpan.Zero,
                PingMode                     = PingMode.Manual,
                CertificateValidationHandler = _options.CertificateValidationHandler
            };

            opts.Standards.RegisterRfc6455();
            opts.Transports.ConfigureTcp(tcpTransport =>
            {
                tcpTransport.NoDelay = true;
                // Dual mode needed for IPv6 support. Does not work with Mono :(
                tcpTransport.DualMode = _options.DualMode;
            });
            _client = new WebSocketClient(opts);
        }
示例#2
0
        internal WebSocketWrapper(Uri baseUri, WebSocketOptions options) : base(options)
        {
            _baseUri        = baseUri;
            _messageReplies = new ConcurrentDictionary <string, TaskCompletionSource <WebSocketMessageEnvelope> >();
            options.ValidateOptions();
            _options = options.Clone();

            if (!IsTrace)
            {
                _options.Logger = new NullLogger();
            }

            OnError = (sender, exception) => _options.Logger.Error(exception);

            OnChannelMessage = (sender, message) =>
                               _options.Logger.DebugFormat("Received channel message '{0}'", message);
            OnChannelPresence = (sender, _event) =>
                                _options.Logger.DebugFormat("Received channel presence '{0}'", _event);
            OnConnect           = (sender, args) => _options.Logger.Debug("Socket connected.");
            OnDisconnect        = (sender, args) => _options.Logger.Debug("Socket disconnected.");
            OnMatchmakerMatched = (sender, matched) =>
                                  _options.Logger.DebugFormat("Received matchmaker match '{0}'", matched);
            OnMatchPresence = (sender, _event) =>
                              _options.Logger.DebugFormat("Received match presence '{0}'", _event);
            OnMatchState   = (sender, state) => _options.Logger.DebugFormat("Received match state '{0}'", state);
            OnNotification = (sender, notification) =>
                             _options.Logger.DebugFormat("Received notification '{0}'", notification);
            OnStatusPresence = (sender, _event) =>
                               _options.Logger.DebugFormat("Received status presence '{0}'", _event);
            OnStreamPresence = (sender, _event) =>
                               _options.Logger.DebugFormat("Received stream presence '{0}'", _event);
            OnStreamState = (sender, state) => _options.Logger.DebugFormat("Received stream state '{0}'", state);

            Connected       += (sender, args) => OnConnect.Invoke(this, EventArgs.Empty);
            Disconnected    += (sender, args) => OnDisconnect.Invoke(this, EventArgs.Empty);
            ErrorReceived   += (sender, exception) => OnError?.Invoke(this, exception);
            MessageReceived += (sender, message) =>
            {
                if (IsTrace)
                {
                    _options.Logger.DebugFormat("Socket read message: '{0}'", message);
                }

                var envelope = message.FromJson <WebSocketMessageEnvelope>();
                if (!string.IsNullOrEmpty(envelope.Cid))
                {
                    // Handle message response.
                    TaskCompletionSource <WebSocketMessageEnvelope> completer;
                    var cid = envelope.Cid;
                    _messageReplies.TryRemove(cid, out completer);
                    if (completer == null)
                    {
                        if (IsTrace)
                        {
                            _options.Logger.InfoFormat("No task completer for message: '{0}'", cid);
                        }
                        return;
                    }

                    if (envelope.Error != null)
                    {
                        // FIXME use a dedicated exception type.
                        completer.SetException(new WebSocketException(envelope.Error.Message));
                    }
                    else
                    {
                        completer.SetResult(envelope);
                    }
                }
                else if (envelope.Error != null)
                {
                    OnError?.Invoke(this, new WebSocketException(envelope.Error.Message));
                }
                else if (envelope.ChannelMessage != null)
                {
                    OnChannelMessage?.Invoke(this, envelope.ChannelMessage);
                }
                else if (envelope.ChannelPresenceEvent != null)
                {
                    OnChannelPresence?.Invoke(this, envelope.ChannelPresenceEvent);
                }
                else if (envelope.MatchmakerMatched != null)
                {
                    OnMatchmakerMatched?.Invoke(this, envelope.MatchmakerMatched);
                }
                else if (envelope.MatchPresenceEvent != null)
                {
                    OnMatchPresence?.Invoke(this, envelope.MatchPresenceEvent);
                }
                else if (envelope.MatchState != null)
                {
                    OnMatchState?.Invoke(this, envelope.MatchState);
                }
                else if (envelope.NotificationList != null)
                {
                    foreach (var notification in envelope.NotificationList.Notifications)
                    {
                        OnNotification?.Invoke(this, notification);
                    }
                }
                else if (envelope.StatusPresenceEvent != null)
                {
                    OnStatusPresence?.Invoke(this, envelope.StatusPresenceEvent);
                }
                else if (envelope.StreamPresenceEvent != null)
                {
                    OnStreamPresence?.Invoke(this, envelope.StreamPresenceEvent);
                }
                else if (envelope.StreamState != null)
                {
                    OnStreamState?.Invoke(this, envelope.StreamState);
                }
                else
                {
                    if (IsTrace)
                    {
                        _options.Logger.InfoFormat("Socket received unrecognised message: '{0}'", message);
                    }
                }
            };
        }