diff --git a/websocket-sharp/Server/WebSocketServiceManager.cs b/websocket-sharp/Server/WebSocketServiceManager.cs index 580db9e0..c0a81fd1 100644 --- a/websocket-sharp/Server/WebSocketServiceManager.cs +++ b/websocket-sharp/Server/WebSocketServiceManager.cs @@ -384,6 +384,98 @@ namespace WebSocketSharp.Server #region Public Methods + /// + /// Adds a WebSocket service with the specified behavior, + /// , and . + /// + /// + /// is converted to a URL-decoded string and + /// / is trimmed from the end of the converted string if any. + /// + /// + /// A that represents an absolute path to + /// the service to add. + /// + /// + /// An Action<TBehavior> delegate that invokes + /// the method used to initialize a new session instance for + /// the service or if not needed. + /// + /// + /// The type of the behavior for the service. It must inherit + /// the class and it must have + /// a public parameterless constructor. + /// + /// + /// is . + /// + /// + /// + /// is empty. + /// + /// + /// -or- + /// + /// + /// is not an absolute path. + /// + /// + /// -or- + /// + /// + /// includes either or both + /// query and fragment components. + /// + /// + /// -or- + /// + /// + /// is already in use. + /// + /// + public void AddService ( + string path, Action 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 = HttpUtility.UrlDecode (path).TrimSlashFromEnd (); + + lock (_sync) { + WebSocketServiceHost host; + if (_hosts.TryGetValue (path, out host)) + throw new ArgumentException ("Already in use.", "path"); + + host = new WebSocketServiceHost ( + path, () => new TBehavior (), initializer, _logger + ); + + if (!_clean) + host.KeepClean = false; + + if (_waitTime != host.WaitTime) + host.WaitTime = _waitTime; + + if (_state == ServerState.Start) + host.Start (); + + _hosts.Add (path, host); + } + } + /// /// Sends binary to every client in the WebSocket services. ///