Просмотр исходного кода

Try to properly close socket connection with the client by sending the handshake leave frame.

Mathieu Barnachon 10 лет назад
Родитель
Сommit
f50c319376
2 измененных файлов с 13 добавлено и 1 удалено
  1. 4 0
      Sources/Socket.swift
  2. 9 1
      Sources/WebSockets.swift

+ 4 - 0
Sources/Socket.swift

@@ -108,6 +108,10 @@ public class Socket: Hashable, Equatable {
         self.socketFileDescriptor = socketFileDescriptor
     }
     
+    deinit {
+        shutdwn()
+    }
+    
     public var hashValue: Int { return Int(self.socketFileDescriptor) }
     
     public func release() {

+ 9 - 1
Sources/WebSockets.swift

@@ -63,6 +63,10 @@ public class WebSocketSession: Hashable, Equatable  {
         self.socket = socket
     }
     
+    deinit {
+        writeCloseFrame()
+    }
+    
     public func writeText(text: String) -> Void {
         self.writeFrame(ArraySlice(text.utf8), OpCode.Text)
     }
@@ -87,6 +91,10 @@ public class WebSocketSession: Hashable, Equatable  {
         }
     }
     
+    private func writeCloseFrame() {
+        writeFrame(ArraySlice("".utf8), .Close)
+    }
+    
     private func encodeLengthAndMaskFlag(len: UInt64, _ masked: Bool) -> [UInt8] {
         let encodedLngth = UInt8(masked ? 0x80 : 0x00)
         var encodedBytes = [UInt8]()
@@ -125,7 +133,7 @@ public class WebSocketSession: Hashable, Equatable  {
         let sec = try socket.read()
         let msk = sec & 0x80 != 0
         guard msk else {
-            // "...a client MUST mask all frames that it sends to the serve.."
+            // "...a client MUST mask all frames that it sends to the server."
             // http://tools.ietf.org/html/rfc6455#section-5.1
             throw Error.UnMaskedFrame
         }