vapor-community / sockets

🔌 Non-blocking TCP socket layer, with event-driven server and client.
MIT License
575 stars 54 forks source link

StreamError: send(“There was a problem while sending data.“, Socket failed with code 9 (“Bad file descriptor”) [sendFailedToSendAllBytes]) #131

Closed JustinM1 closed 6 years ago

JustinM1 commented 7 years ago

I have a webpage that once the document is ready, starts a socket connection. The client only receives messages on a continuous basis and does not send any. I noticed an issue where if I refresh the page, I.E. closing the current socket and instantly opening a new one, I would get the error stated in the title on any attempts to send data to client.

Upon doing some research I found this example which locks onClose: https://github.com/IBM-Swift/Kitura-Chat-Server/blob/master/Sources/KituraChatServer/ChatService.swift

This resolved the issue. FYI this was using Vapor 1. Not sure if something has already been put in place with Vapor 2 to handle this situation. Sample code I used is below.

private let connectionsLock = DispatchSemaphore(value: 1)

ws.onClose = { [weak self] ws,_,_,_ in
            self?.lockConnectionsLock()
              //remove connection logic
              self?.unlockConnectionsLock()
}

  private func lockConnectionsLock() {
    _ = connectionsLock.wait(timeout: DispatchTime.distantFuture)
  }

  private func unlockConnectionsLock() {
    connectionsLock.signal()
  }
tanner0101 commented 6 years ago

Vapor 1 is no longer being supported. Sorry for the inconvenience.