فهرست منبع

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

Mathieu Barnachon 10 سال پیش
والد
کامیت
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
         self.socketFileDescriptor = socketFileDescriptor
     }
     }
     
     
+    deinit {
+        shutdwn()
+    }
+    
     public var hashValue: Int { return Int(self.socketFileDescriptor) }
     public var hashValue: Int { return Int(self.socketFileDescriptor) }
     
     
     public func release() {
     public func release() {

+ 9 - 1
Sources/WebSockets.swift

@@ -63,6 +63,10 @@ public class WebSocketSession: Hashable, Equatable  {
         self.socket = socket
         self.socket = socket
     }
     }
     
     
+    deinit {
+        writeCloseFrame()
+    }
+    
     public func writeText(text: String) -> Void {
     public func writeText(text: String) -> Void {
         self.writeFrame(ArraySlice(text.utf8), OpCode.Text)
         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] {
     private func encodeLengthAndMaskFlag(len: UInt64, _ masked: Bool) -> [UInt8] {
         let encodedLngth = UInt8(masked ? 0x80 : 0x00)
         let encodedLngth = UInt8(masked ? 0x80 : 0x00)
         var encodedBytes = [UInt8]()
         var encodedBytes = [UInt8]()
@@ -125,7 +133,7 @@ public class WebSocketSession: Hashable, Equatable  {
         let sec = try socket.read()
         let sec = try socket.read()
         let msk = sec & 0x80 != 0
         let msk = sec & 0x80 != 0
         guard msk else {
         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
             // http://tools.ietf.org/html/rfc6455#section-5.1
             throw Error.UnMaskedFrame
             throw Error.UnMaskedFrame
         }
         }