Exemplo n.º 1
0
        public WebSocketInterface(int port, Session session)
        {
            _session = session;
            var translations = session.Translation;
            _server = new WebSocketServer();
            _websocketHandler = WebSocketEventManager.CreateInstance();
            var setupComplete = _server.Setup(new ServerConfig
            {
                Name = "NecroWebSocket",
                Ip = "Any",
                Port = port,
                Mode = SocketMode.Tcp,
                Security = "tls",
                Certificate = new CertificateConfig
                {
                    FilePath = @"cert.pfx",
                    Password = "******"
                }
            });

            if (setupComplete == false)
            {
                Logger.Write(translations.GetTranslation(TranslationString.WebSocketFailStart, port), LogLevel.Error);
                return;
            }

            _server.NewMessageReceived += HandleMessage;
            _server.NewSessionConnected += HandleSession;

            _server.Start();
        }
        // Registers all IWebSocketRequestHandler's automatically.

        public static WebSocketEventManager CreateInstance()
        {
            var manager = new WebSocketEventManager();

            var type  = typeof(IWebSocketRequestHandler);
            var types = AppDomain.CurrentDomain.GetAssemblies()
                        .SelectMany(s => s.GetTypes())
                        .Where(p => type.IsAssignableFrom(p) && p.IsClass);

            foreach (var plugin in types)
            {
                IWebSocketRequestHandler instance = (IWebSocketRequestHandler)Activator.CreateInstance(plugin);
                manager.RegisterHandler(instance.Command, instance);
            }

            return(manager);
        }
Exemplo n.º 3
0
        // Registers all IWebSocketRequestHandler's automatically. 

        public static WebSocketEventManager CreateInstance()
        {
            var manager = new WebSocketEventManager();

            var type = typeof(IWebSocketRequestHandler);
            var types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => type.IsAssignableFrom(p) && p.IsClass);

            foreach(var plugin in types)
            {
                IWebSocketRequestHandler instance = (IWebSocketRequestHandler)Activator.CreateInstance(plugin);
                manager.RegisterHandler(instance.Command, instance);
            }

            return manager;
        }
Exemplo n.º 4
0
        public WebSocketInterface(string ipAddress, int port, Session session)
        {
            _session          = session;
            _server           = new WebSocketServer();
            _websocketHandler = WebSocketEventManager.CreateInstance();

            // Add custom seriaizer to convert ulong to string (ulong shoud not appear to json according to json specs)
            _jsonSerializerSettings = new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.All
            };
            _jsonSerializerSettings.Converters.Add(new LongToStringJsonConverter());

            ITranslation translations = session.Translation;

            ServerConfig config = new ServerConfig
            {
                Name        = "NecroWebSocket",
                Mode        = SocketMode.Tcp,
                Certificate = new CertificateConfig {
                    FilePath = @"cert.pfx", Password = "******"
                },
                Listeners =
                    new List <ListenerConfig>
                {
                    new ListenerConfig {
                        Ip = ipAddress, Port = port, Security = "tls"
                    },
                    new ListenerConfig {
                        Ip = ipAddress, Port = port + 1, Security = "none"
                    }
                },
            };

            bool setupComplete = _server.Setup(config);

            if (setupComplete == false)
            {
                Logger.Write(translations.GetTranslation(TranslationString.WebSocketFailStart, port), LogLevel.Error);
                return;
            }

            _server.NewMessageReceived  += HandleMessage;
            _server.NewSessionConnected += HandleSession;

            _server.Start();
        }