internal void Add <TBehavior>(string path, Func <TBehavior> initializer) where TBehavior : WebSocketBehavior { lock (this._sync) { WebSocketServiceHost host; path = HttpUtility.UrlDecode(path).TrimEndSlash(); if (this._hosts.TryGetValue(path, out host)) { this._logger.Error("A WebSocket service with the specified path already exists:\n path: " + path); } else { host = new WebSocketServiceHost <TBehavior>(path, initializer, this._logger); if (!this._clean) { host.KeepClean = false; } if (this._waitTime != host.WaitTime) { host.WaitTime = this._waitTime; } if (this._state == 1) { host.Start(); } this._hosts.Add(path, host); } } }
static void Main(string[] args) { LogConsole._Load(); LogDebug.debug = true; _SQL.Init("localhost", "root", "13456", "dbbase", 3306); var wssv = new WebSocketServiceHost<Serverb>("ws://192.168.1.5:8085"); wssv.OnError += (sender, e) => { Console.WriteLine("[WS] error", "WS: Error: " + e.Message, "notification-message-im"); }; wssv.Start(); Console.WriteLine("WebSocket Server listening on port: {0}", wssv.Port); while (true) { Thread.Sleep(1000); string _comm = Console.ReadLine(); switch (_comm) { case "count": Console.WriteLine("Users Online: {0}",Users.Count()); break; default: break; } } //Console.ReadKey(); }
internal void Add <TBehavior> (string path, Func <TBehavior> initializer, object callerContext) where TBehavior : WebSocketBehavior { lock (_sync) { path = HttpUtility.UrlDecode(path).TrimEndSlash(); WebSocketServiceHost host; if (_hosts.TryGetValue(path, out host)) { _logger.Error( "A WebSocket service with the specified path already exists:\n path: " + path); return; } host = new WebSocketServiceHost <TBehavior> (path, initializer, _logger); host.CallerContext = callerContext; if (!_clean) { host.KeepClean = false; } if (_waitTime != host.WaitTime) { host.WaitTime = _waitTime; } if (_state == ServerState.Start) { host.Start(); } _hosts.Add(path, host); } }
internal void Add <TBehavior> (string path, Func <TBehavior> creator) where TBehavior : WebSocketBehavior { path = HttpUtility.UrlDecode(path).TrimSlashFromEnd(); lock (_sync) { WebSocketServiceHost host; if (_hosts.TryGetValue(path, out host)) { throw new ArgumentException("Already in use.", "path"); } host = new WebSocketServiceHost <TBehavior> ( path, creator, null, _log ); if (!_clean) { host.KeepClean = false; } if (_waitTime != host.WaitTime) { host.WaitTime = _waitTime; } if (_state == ServerState.Start) { host.Start(); } _hosts.Add(path, host); } }
internal void Add <TBehavior>(string path, Func <TBehavior> initializer) where TBehavior : WebSocketBehavior { path = HttpUtility.UrlDecode(path).TrimEndSlash(); WebSocketServiceHost host; if (_hosts.TryGetValue(path, out host)) { return; } host = new WebSocketServiceHost <TBehavior>(path, _fragmentSize, initializer); if (!_clean) { host.KeepClean = false; } host.WaitTime = _waitTime; if (_state == ServerState.Start) { host.Start(); } if (!_hosts.TryAdd(path, host)) { throw new Exception("Failed to add host"); } }
internal void Add <TBehavior>(string path, Func <TBehavior> initializer) where TBehavior : WebSocketBehavior { lock (_sync) { path = HttpUtility.UrlDecode(path).TrimEndSlash(); if (_hosts.TryGetValue(path, out var value)) { _logger.Error("A WebSocket service with the specified path already exists.\npath: " + path); return; } value = new WebSocketServiceHost <TBehavior>(path, initializer, _logger); if (!_clean) { value.KeepClean = false; } if (_waitTime != value.WaitTime) { value.WaitTime = _waitTime; } if (_state == ServerState.Start) { value.Start(); } _hosts.Add(path, value); } }
/// <summary> /// Adds a WebSocket service with the specified behavior, path, /// and delegate. /// </summary> /// <param name="path"> /// <para> /// A <see cref="string"/> that represents an absolute path to /// the service to add. /// </para> /// <para> /// / is trimmed from the end of the string if present. /// </para> /// </param> /// <param name="initializer"> /// <para> /// An <c>Action<TBehavior></c> delegate or /// <see langword="null"/> if not needed. /// </para> /// <para> /// The delegate invokes the method called when initializing /// a new session instance for the service. /// </para> /// </param> /// <typeparam name="TBehavior"> /// <para> /// The type of the behavior for the service. /// </para> /// <para> /// It must inherit the <see cref="WebSocketBehavior"/> class. /// </para> /// <para> /// And also, it must have a public parameterless constructor. /// </para> /// </typeparam> /// <exception cref="ArgumentNullException"> /// <paramref name="path"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <para> /// <paramref name="path"/> is empty. /// </para> /// <para> /// -or- /// </para> /// <para> /// <paramref name="path"/> is not an absolute path. /// </para> /// <para> /// -or- /// </para> /// <para> /// <paramref name="path"/> includes either or both /// query and fragment components. /// </para> /// <para> /// -or- /// </para> /// <para> /// <paramref name="path"/> is already in use. /// </para> /// </exception> public void AddService <TBehavior>( string path, Action <TBehavior> initializer ) where TBehavior : WebSocketBehavior, new() { if (path == null) { throw new ArgumentNullException("path"); } if (path.Length == 0) { throw new ArgumentException("An empty string.", "path"); } if (path[0] != '/') { throw new ArgumentException("Not an absolute path.", "path"); } if (path.IndexOfAny(new[] { '?', '#' }) > -1) { var msg = "It includes either or both query and fragment components."; throw new ArgumentException(msg, "path"); } path = path.TrimSlashFromEnd(); lock (_sync) { WebSocketServiceHost host; if (_hosts.TryGetValue(path, out host)) { throw new ArgumentException("Already in use.", "path"); } host = new WebSocketServiceHost <TBehavior>( path, () => new TBehavior(), initializer, _log ); if (!_clean) { host.KeepClean = false; } if (_waitTime != host.WaitTime) { host.WaitTime = _waitTime; } if (_state == ServerState.Start) { host.Start(); } _hosts.Add(path, host); } }
internal void Add <TBehavior>(string path, Func <TBehavior> initializer) where TBehavior : WebSocketBehavior { WebSocketServiceHost webSocketServiceHost; object obj = this._sync; Monitor.Enter(obj); try { path = HttpUtility.UrlDecode(path).TrimEndSlash(); if (!this._hosts.TryGetValue(path, out webSocketServiceHost)) { webSocketServiceHost = new WebSocketServiceHost <TBehavior>(path, initializer, this._logger); if (!this._clean) { webSocketServiceHost.KeepClean = false; } if (this._waitTime != webSocketServiceHost.WaitTime) { webSocketServiceHost.WaitTime = this._waitTime; } if (this._state == ServerState.Start) { webSocketServiceHost.Start(); } this._hosts.Add(path, webSocketServiceHost); } else { this._logger.Error(string.Concat("A WebSocket service with the specified path already exists:\n path: ", path)); } } finally { Monitor.Exit(obj); } }
static void Main(string[] args) { LogConsole._Load(); PATH = Environment.CurrentDirectory.ToString(); Inix ini = null; #region Load Settings try { if (File.Exists(PATH + @"\Settings\Settings.ini")) { ini = new Inix(PATH + @"\Settings\Settings.ini"); LSPort = ini.GetValue("Server", "port", 9002).ToString(); LSIP = ini.GetValue("Server", "ip", "localhost").ToString(); MIp = ini.GetValue("MySql", "ip", "localhost").ToString(); MUser = ini.GetValue("MySql", "user", "root").ToString(); MPass = ini.GetValue("MySql", "pass", "").ToString(); MDb = ini.GetValue("MySql", "db", "db_clone").ToString(); MPort = Convert.ToInt32(ini.GetValue("MySql", "port", 3306)); debug = Convert.ToBoolean(ini.GetValue("Console", "debug", false)); ini = null; LogConsole.Show(LogType.INFO, "Has loaded your ip settings successfully"); } else { LogConsole.Show(LogType.ALERT, "Settings.ini could not be found, using default setting"); } } catch (Exception excc) { LogConsole.Show(LogType.ERROR, " {0}", excc.ToString()); return; } #endregion _SQL.Init(MIp, MUser, MPass, MDb, MPort); MapsL.LoadMaps.Load(); WSurl = WSurl + LSIP + ":" + LSPort; var wssv = new WebSocketServiceHost<Serverb>(WSurl); wssv.OnError += (sender, e) => { LogConsole.Show(LogType.ERROR, "[WS]: Error {0} ", e.Message); }; wssv.Start(); LogConsole.Show(LogType.ALERT, "Server Listening on port: {0}", wssv.Port); _LoopThrreading = new Thread(new ThreadStart(Program.LoopConsole)); _LoopThrreading.Priority = ThreadPriority.BelowNormal; _LoopThrreading.Start(); while (true) { Thread.Sleep(1000); string _comm = Console.ReadLine(); switch (_comm) { case "online": LogConsole.Show(LogType.INFO, "Users Online: {0}", Users.Count()); break; default: break; } } }