Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I figured out that the delegate is never called, because the websocket is never really closed. The closing of the websocket in the SRWebSocket happens in the method <strong>pumpWriting</strong> like this:</p> <pre><code>if (_closeWhenFinishedWriting &amp;&amp; _outputBuffer.length - _outputBufferOffset == 0 &amp;&amp; (_inputStream.streamStatus != NSStreamStatusNotOpen &amp;&amp; _inputStream.streamStatus != NSStreamStatusClosed) &amp;&amp; !_sentClose) { _sentClose = YES; [_outputStream close]; [_inputStream close]; if (!_failed) { dispatch_async(_callbackQueue, ^{ if ([self.delegate respondsToSelector:@selector(webSocket:didCloseWithCode:reason:wasClean:)]) { [self.delegate webSocket:self didCloseWithCode:_closeCode reason:_closeReason wasClean:YES]; } }); } _selfRetain = nil; NSLog(@" Is really closed and released "); } else { NSLog(@" Is NOT closed and released "); } </code></pre> <p>All streams and an object to retain the websocket are closed or deleted there. As long as they are still open, the socket won´t be closed appropriately. But the closing never happened in my program, because when I tried to close the websocket, <strong>_closeWhenFinishedWriting</strong> was always NO. </p> <p>This boolean is only set once in the <strong>disconnect</strong> method.</p> <pre><code>- (void)_disconnect; { assert(dispatch_get_current_queue() == _workQueue); SRFastLog(@"Trying to disconnect"); _closeWhenFinishedWriting = YES; [self _pumpWriting]; } </code></pre> <p>But when calling the <strong>closeWithCode</strong> method in SRWebSocket, <strong>disconnect</strong> is only called in one case and that is, if the websocket is in the connecting state. </p> <pre><code>BOOL wasConnecting = self.readyState == SR_CONNECTING; SRFastLog(@"Closing with code %d reason %@", code, reason); dispatch_async(_workQueue, ^{ if (wasConnecting) { [self _disconnect]; return; } </code></pre> <p>This means, if the socket is in another state, the websocket will never really close. One workaround is to always call the <strong>disconnect</strong> method. At least it worked for me and everything seems to be alright.</p> <p>If anyone has an idea, why SRWebSocket is implemented like that, please leave a comment for this answer and help me out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload