Unified the ways of message thread in WebSocket.cs
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							@@ -73,9 +73,9 @@ namespace Example
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      ThreadPool.QueueUserWorkItem(notifyMsg);
 | 
					      ThreadPool.QueueUserWorkItem(notifyMsg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      //using (WebSocket ws = new WebSocket("ws://echo.websocket.org", "echo"))
 | 
					      using (WebSocket ws = new WebSocket("ws://echo.websocket.org", "echo"))
 | 
				
			||||||
      //using (WebSocket ws = new WebSocket("wss://echo.websocket.org", "echo"))
 | 
					      //using (WebSocket ws = new WebSocket("wss://echo.websocket.org", "echo"))
 | 
				
			||||||
      using (WebSocket ws = new WebSocket("ws://localhost:4649"))
 | 
					      //using (WebSocket ws = new WebSocket("ws://localhost:4649"))
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        ws.OnOpen += (sender, e) =>
 | 
					        ws.OnOpen += (sender, e) =>
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 
 | 
				
			|||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							@@ -66,14 +66,13 @@ namespace WebSocketSharp
 | 
				
			|||||||
    private string                          _binaryType;
 | 
					    private string                          _binaryType;
 | 
				
			||||||
    private HttpListenerWebSocketContext    _context;
 | 
					    private HttpListenerWebSocketContext    _context;
 | 
				
			||||||
    private IPEndPoint                      _endPoint;
 | 
					    private IPEndPoint                      _endPoint;
 | 
				
			||||||
    private AutoResetEvent                  _exitedMessageLoop;
 | 
					    private AutoResetEvent                  _exitMessageLoop;
 | 
				
			||||||
    private string                          _extensions;
 | 
					    private string                          _extensions;
 | 
				
			||||||
    private Object                          _forClose;
 | 
					    private Object                          _forClose;
 | 
				
			||||||
    private Object                          _forSend;
 | 
					    private Object                          _forSend;
 | 
				
			||||||
    private int                             _fragmentLen;
 | 
					    private int                             _fragmentLen;
 | 
				
			||||||
    private bool                            _isClient;
 | 
					    private bool                            _isClient;
 | 
				
			||||||
    private bool                            _isSecure;
 | 
					    private bool                            _isSecure;
 | 
				
			||||||
    private Thread                          _msgThread;
 | 
					 | 
				
			||||||
    private NetworkStream                   _netStream;
 | 
					    private NetworkStream                   _netStream;
 | 
				
			||||||
    private string                          _protocol;
 | 
					    private string                          _protocol;
 | 
				
			||||||
    private string                          _protocols;
 | 
					    private string                          _protocols;
 | 
				
			||||||
@@ -355,13 +354,8 @@ namespace WebSocketSharp
 | 
				
			|||||||
    private void closeHandshake(WsFrame frame)
 | 
					    private void closeHandshake(WsFrame frame)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      if (send(frame) && !Thread.CurrentThread.IsBackground)
 | 
					      if (send(frame) && !Thread.CurrentThread.IsBackground)
 | 
				
			||||||
      {
 | 
					        if (_exitMessageLoop != null)
 | 
				
			||||||
        if (_isClient && _msgThread != null)
 | 
					          _exitMessageLoop.WaitOne(5 * 1000);
 | 
				
			||||||
          _msgThread.Join(5 * 1000);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!_isClient && _exitedMessageLoop != null)
 | 
					 | 
				
			||||||
          _exitedMessageLoop.WaitOne(5 * 1000);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      ReadyState = WsState.CLOSED;
 | 
					      ReadyState = WsState.CLOSED;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -673,14 +667,6 @@ namespace WebSocketSharp
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void messageLoop()
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
      while (_readyState == WsState.OPEN)
 | 
					 | 
				
			||||||
      {
 | 
					 | 
				
			||||||
        message();
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private void messageLoopCallback(IAsyncResult ar)
 | 
					    private void messageLoopCallback(IAsyncResult ar)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      Action messageInvoker = (Action)ar.AsyncState;
 | 
					      Action messageInvoker = (Action)ar.AsyncState;
 | 
				
			||||||
@@ -689,11 +675,10 @@ namespace WebSocketSharp
 | 
				
			|||||||
      if (_readyState == WsState.OPEN)
 | 
					      if (_readyState == WsState.OPEN)
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        messageInvoker.BeginInvoke(messageLoopCallback, messageInvoker);
 | 
					        messageInvoker.BeginInvoke(messageLoopCallback, messageInvoker);
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      else
 | 
					
 | 
				
			||||||
      {
 | 
					      _exitMessageLoop.Set();
 | 
				
			||||||
        _exitedMessageLoop.Set();
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void pong(PayloadData data)
 | 
					    private void pong(PayloadData data)
 | 
				
			||||||
@@ -1042,25 +1027,16 @@ namespace WebSocketSharp
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private void startMessageThread()
 | 
					    private void startMessageThread()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      if (_isClient)
 | 
					      _exitMessageLoop = new AutoResetEvent(false);
 | 
				
			||||||
      {
 | 
					
 | 
				
			||||||
        _msgThread = new Thread(new ThreadStart(messageLoop)); 
 | 
					 | 
				
			||||||
        _msgThread.IsBackground = true;
 | 
					 | 
				
			||||||
        _msgThread.Start();
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      else
 | 
					 | 
				
			||||||
      {
 | 
					 | 
				
			||||||
        _exitedMessageLoop = new AutoResetEvent(false);
 | 
					 | 
				
			||||||
      Action messageInvoker = () =>
 | 
					      Action messageInvoker = () =>
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        if (_readyState == WsState.OPEN)
 | 
					        if (_readyState == WsState.OPEN)
 | 
				
			||||||
          {
 | 
					 | 
				
			||||||
          message();
 | 
					          message();
 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      messageInvoker.BeginInvoke(messageLoopCallback, messageInvoker);
 | 
					      messageInvoker.BeginInvoke(messageLoopCallback, messageInvoker);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #endregion
 | 
					    #endregion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user