0001    // Client.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    @_exported import HTTPClient
0026    @_exported import HTTPSClient
0027    @_exported import Venice
0028    @_exported import OpenSSL
0029    
0030    public struct Client
Client__.swift:49
    public func connectInBackground(path: String, failure: ErrorType -> Void = Client.logError) {
{ 0031 0032 public enum Error: ErrorType { 0033 case NoRequest 0034 case ResponseNotWebsocket 0035 } 0036 0037 private let client: ClientType 0038 private let onConnect
Client__.swift:46
        self.onConnect =  onConnect
: Socket throws -> Void 0039 0040 public init(ssl: Bool, host: String, port: Int, onConnect: Socket throws -> Void) throws { 0041 if ssl { 0042 self.client = try HTTPSClient.Client(host: host, port: port) 0043 } else { 0044 self.client = try HTTPClient.Client(host: host, port: port) 0045 } 0046 self.onConnect = onConnect 0047 } 0048 0049 public func connectInBackground(path: String, failure: ErrorType -> Void = Client.logError) { 0050 co { 0051 do { 0052 try self.connect(path) 0053 } catch { 0054 failure(error) 0055 } 0056 } 0057 } 0058 0059 static func logError
Client__.swift:49
    public func connectInBackground(path: String, failure: ErrorType -> Void = Client.logError) {
(error: ErrorType) { 0060 print(error) 0061 } 0062 0063 public func connect(path: String) throws { 0064 let key = try Base64.encode(Random.getBytes(16)) 0065 0066 let headers: Headers = [ 0067 "Connection": "Upgrade", 0068 "Upgrade": "websocket", 0069 "Sec-WebSocket-Version": "13", 0070 "Sec-WebSocket-Key": key, 0071 ] 0072 0073 var _request: Request? 0074 let request = try Request(method: .GET, uri: path, headers: headers) { response, stream in 0075 guard let request = _request else { 0076 throw Error.NoRequest 0077 } 0078 0079 guard response.status == .SwitchingProtocols && response.isWebSocket else { 0080 throw Error.ResponseNotWebsocket 0081 } 0082 0083 guard let accept = response.webSocketAccept where accept == Socket.accept(key) else { 0084 throw Error.ResponseNotWebsocket 0085 } 0086 0087 let webSocket = Socket(stream: stream, mode: .Client, request: request, response: response) 0088 try self.onConnect(webSocket) 0089 try webSocket.loop() 0090 } 0091 _request = request 0092 0093 try client.send(request) 0094 } 0095 0096 } 0097