Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WebSocketをiOSに持ち込んで辛い思いをした経験がありますか!? / have you...

taminif
September 01, 2018

WebSocketをiOSに持ち込んで辛い思いをした経験がありますか!? / have you painful experience in web socket?

iOSDC 2018 9/1 TrackC 16:00

taminif

September 01, 2018
Tweet

More Decks by taminif

Other Decks in Programming

Transcript

  1. ࣮૷͢Δ import SocketIO let client = SocketIOClient( socketURL: URL(string: "wss://example.com")!,

    config: [ .path("/socket-io/socket.io"), .log(true), .connectParams(["device": "IOS", "token": "test_token"]) ] ) client.connect() client.on(clientEvent: .connect) {_, _ in callback() }
  2. σʔλΛૹ৴͢Δ func emit(_ event: String, _ items: SocketData...) { try?

    client.emit( event, with: items.map( {try $0.socketRepresentation()} ) ) } -> ͜ΕͰσʔλૹ৴·Ͱ֬ೝͰ͖ͨ ૹΔ࣌ʹҰ౓ϑΝΠϧܗࣜʹม׵͢Δඞཁ͋Γ
  3. ͜ͷ஌ݟ͕ͳ͔ͬͨࠒ ௐࠪΛߦ͏ import SocketIO let client = SocketIOClient( socketURL: URL(string:

    "wss://example.com")!, config: [ .path("/socket-io/socket.io"), .log(true), .connectParams(["device": "IOS", "token": "test_token"]) ] ) ௐࠪ͢ΔͨΊʹϩάΛग़ྗ͢Δͱɺ 8FC4PDLFUʹࣦഊͨ࣌͠ʹ )551઀ଓ͢ΔڍಈΛൃݟͨ͠
  4. import SocketIO let client = SocketIOClient( socketURL: URL(string: "wss://example.com")!, config:

    [ .path("/socket-io/socket.io"), .forceWebsockets(true), .log(true), .connectParams(["device": "IOS", "token": "test_token"]) ] ) ࢓ํͳ͍ͷͰಥ؏޻ࣄͰରԠ WebSocketΛڧ੍͢Δ ௨৴Τϥʔ࣌ͷ࠶઀ଓ౳Λ શͯ8FC4PDLFUʹͯ͘͠ΕΔ
  5. disconnectͰඞͣ઀ଓΛด͡Δ func disconnect() { client.disconnect() } func isConnected() -> Bool

    { guard let client = client else { return false } return client.status == SocketIOClientStatus.connected } ઀ଓΛด͡Δ ઀ଓεςʔλεΛ͍࣋ͬͯΔͷͰ ઀ଓத͔֬ೝͰ͖Δ
  6. scheduledTimerͰ͸ͳ͘ RunLoopΫϥεΛ࢖༻͢Δ self.recordChunkTimer = Timer( timeInterval: 5.0, target: self, selector:

    #selector(self.chunkTimerEvent), userInfo: event, repeats: true ) RunLoop.current.add( self.recordChunkTimer, forMode: .commonModes )
  7. emitΛϝΠϯεϨουͰಈ͘Α͏ʹ func emit(_ event: String, _ items: SocketData...) { DispatchQueue.main.async

    { try? client.emit( event, with: items.map( {try $0.socketRepresentation()} ) ) } } -> վળ͞Εͳ͔ͬͨ
  8. socket.io͸Manager಺ʹ εϨουΛ͍࣋ͬͯͯɺͦͷεϨουΛ࢖༻͢Δ SocketManager.swift /// The queue that all interaction with

    the client should occur on. This is the queue that event handlers are /// called on. /// /// **This should be a serial queue! Concurrent queues are not supported and might cause crashes and races**. public var handleQueue = DispatchQueue.main https://github.com/socketio/socket.io-client-swift/blob/d03e319e3573f61e77e6c16461ff96b2f0fe1f79/Source/SocketIO/Manager/SocketManager.swift