コード例 #1
0
ファイル: WebSocket.cs プロジェクト: vzolotov/WebSharp
        /// <summary>
        /// Closes the connection to the WebSocket server
        /// </summary>
        /// <param name="closeCode">The WebSocket close status</param>
        /// <param name="reason">A description of the close status.</param>
        /// <returns></returns>
        public PPError Close(WebSocketCloseStatus closeCode, string reason = null)
        {
            ThrowIfNotConnected();

            return((PPError)PPBWebSocket.Close(this, (ushort)closeCode,
                                               string.IsNullOrEmpty(reason) ? null : new Var(reason),
                                               new CompletionCallback(OnClose)));
        }
コード例 #2
0
ファイル: WebSocket.cs プロジェクト: vzolotov/WebSharp
        private async Task <PPError> CloseAsyncCore(WebSocketCloseStatus closeCode, string reason = null, MessageLoop closeLoop = null)
        {
            var tcs = new TaskCompletionSource <PPError>();
            EventHandler <PPError> handler = (s, e) => { tcs.TrySetResult(e); };

            try
            {
                Closed += handler;

                if (MessageLoop == null && closeLoop == null)
                {
                    Close(closeCode, reason);
                }
                else
                {
                    Action <PPError> action = new Action <PPError>((e) =>
                    {
                        var result = (PPError)PPBWebSocket.Close(this, (ushort)closeCode,
                                                                 string.IsNullOrEmpty(reason) ? null : new Var(reason),
                                                                 new BlockUntilComplete()
                                                                 );
                        tcs.TrySetResult(result);
                    }
                                                                   );
                    InvokeHelper(action, closeLoop);
                }
                return(await tcs.Task);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
                tcs.SetException(exc);
                return(PPError.Aborted);
            }
            finally
            {
                Closed -= handler;
            }
        }