Esempio n. 1
0
        private NClient(string serverKey)
        {
            ConnectTimeout = 3000;
            Host           = "127.0.0.1";
            Port           = 7350;
            ServerKey      = serverKey;
            SSL            = false;
            Timeout        = 5000;
            Trace          = false;
            Lang           = "en";
#if UNITY
            // NOTE Not compiled by default; avoids dependency on UnityEngine
            Logger = new NUnityLogger();
#else
            Logger = new NConsoleLogger();
#endif

#if UNITY_WEBGL && !UNITY_EDITOR
            transport = new NTransportJavascript();
#else
            transport = new NTransport();
#endif

            transport.Logger   = Logger;
            transport.Trace    = Trace;
            transport.OnClose += (sender, _) =>
            {
                collationIds.Clear();
                OnDisconnect.Emit(this, EventArgs.Empty);
            };
            transport.OnMessage += (sender, m) =>
            {
                var message = Envelope.Parser.ParseFrom(m.Data);
                Logger.TraceFormatIf(Trace, "SocketDecoded: {0}", message);
                onMessage(message);
            };
        }
Esempio n. 2
0
        private NClient(string serverKey, TransportType transportType)
        {
            // Don't send Expect: 100 Continue when sending HTTP requests
            ServicePointManager.Expect100Continue = false;
            ServicePointManager.ServerCertificateValidationCallback += ValidateCertificate;

            TransportType  = transportType;
            ConnectTimeout = 3000;
            Host           = "127.0.0.1";
            Port           = 7350;
            ServerKey      = serverKey;
            SSL            = false;
            // Although this setting is not recommended, default to previous behaviour
            SSLAcceptAllCertificates = true;
            Timeout = 5000;
            Trace   = false;
            Lang    = "en";
#if UNITY
            // NOTE Not compiled by default; avoids dependency on UnityEngine
            Logger = new NUnityLogger();
#else
            Logger = new NConsoleLogger();
#endif

#if UNITY_WEBGL && !UNITY_EDITOR
            transport = new NTransportJavascript();
#else
            if (transportType == TransportType.Udp)
            {
                transport = new NTransportUdp();
            }
            else
            {
                // Default to WebSocket transport.
                transport = new NTransportWebSocket();
            }
#endif

            transport.Logger = Logger;
            transport.Trace  = Trace;
            transport.SetOnClose((args) =>
            {
                collationIds.Clear();
                if (OnDisconnect != null)
                {
                    OnDisconnect(new NDisconnectEvent(args.Code, args.Reason));
                }
            });
            transport.SetOnMessage((m) =>
            {
                var message = Envelope.Parser.ParseFrom(m.Data);
                Logger.TraceFormatIf(Trace, "SocketDecoded: {0}", message);
                onMessage(message);
            });
            transport.SetOnError((m) =>
            {
                if (OnError != null)
                {
                    var message = (m.Error != null) ? m.Error.Message : "A transport error occured.";
                    OnError(new NError(message));
                }
            });
        }