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

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

73e80d16abc7cc65e08d430c2eae811b?s=47 taminif
September 01, 2018

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

iOSDC 2018 9/1 TrackC 16:00

73e80d16abc7cc65e08d430c2eae811b?s=128

taminif

September 01, 2018
Tweet

Transcript

  1. WebSocketΛiOSʹ࣋ͪࠐΜͰ ਏ͍ࢥ͍Λͨ͠ܦݧ͕ ͋Γ·͔͢!? iOSDC Japan 2018 Track C 2018/09/01 େౡ

    ޫو@sbntaminif
  2. ࣗݾ঺հ ΢ΣϒϦΦגࣜձࣾWEBΤϯδχΞ
 + ϓϩμΫτϚωʔδϟʔ / ࠷ۙ͸΄ͱΜͲίʔυॻ͍ͯͳ͍ ؔ੢ͷษڧձͷओ࠵΍ͬͯ·͢
 WebRTC Meetup +

    SkyWay Meetup + ؔ੢NodeֶԂ ژ౎Ͱಇ͍͍ͯ·͢ Apple Storeژ౎ߦ͖ͬͯ·ͨ͠ ->
  3. https://ejje.weblio.jp/content/iOS?dictCode=WEJTY

  4. None
  5. iOSDC 2017 https://speakerdeck.com/taminif/ying-hui-hua-sabisufalsetamenikai-fa-sitabideotiyatutoapurifalseji-shu

  6. ڈ೥ͷ͓͞Β͍Λ1෼Ͱ WebRTC Firebase WkWebView Web/iOS

  7. ڈ೥ΑΓ޷ධ!! http://buffalo.jp/jirei/education/seibou.html#tab_kouka ͋Γ͕͍ͨ͜ͱʹ ֶߍݱ৔Ͱͷಋೖ͕ ૿͖͑ͯ·ͨ͠ ཁ๬Λ௖͖ ৽͍͠ػೳΛ ௥Ճ ͦΕ͕ɾɾ

  8. Ի੠࿥Իػೳ ※࣮ࡍ͸γεςϜԽͯ͠·͢ Ϩοεϯͷ ෮श ੒੷ͷ ධՁ

  9. ϩʔΧϧʹ࿥Ի͠ ϑΝΠϧΛૹ৴͢Δ WebRTC ૹ৴ΛͲ͏΍Δ͔

  10. WebSocketΛ࢖͏ • ͍ΘΏΔωοτϫʔΫϓϩτίϧ • αʔόʔͱΫϥΠΞϯτͰ
 ૒ํ޲ʹૹ৴͢Δٕज़ • ୺຤ෛՙɾίωΫγϣϯอ࣋ͷ
 ͲͪΒ΋௿ίετ! Firebase΋

    WebSocket WebSocket ͱ͸
  11. ͳͥWebSocketΛબΜ͔ͩ ʢେલఏʣ Ϩοεϯ͕࠷༏ઌ ୺຤ͷෛՙΛ཈͑Δ ֶߍʹΑͬͯѻ͏୺຤͸ҧ͏ ϑΟϦϐϯͷωοτϫʔΫΛ ߟྀ ·ͩ·ͩΠϯϑϥ͸੔උ͞Ε͍ͯͳ͍

  12. ྫ͑͹WebRTCͰ࿥Ի • ίωΫγϣϯΛೋͭுΔඞཁ͕͋Δ • WebRTC͸୺຤ʹ݁ߏͳෛՙ͕͔͔Δ • Ի੠औಘɾωοτϫʔΫ྆ํ • Ϩοεϯͷө૾ɾԻ੠͕ཚΕΔՄೳੑ͕͋Δ

  13. Ͳ͏͍͏ߏ੒Ͱ ࿥Ի͢Δ͔

  14. ߨࢣͱੜెͷ੠ΛͦΕͧΕ࿥Ի

  15. WebͱiOSͷ྆ํʹରԠ WebM AAC AWS

  16. 5ඵ͝ͱʹϑΝΠϧΛૹ৴ 5ඵ͝ͱͷԻ੠ WebMͷΈઌ಄ϑΝΠϧʹ ϔομʔ͋Γ WebSocketίωΫγϣϯ ड͚औͬͨԻ੠͸ Ұ୴S3ʹ֨ೲ ཌਂ໷ʹ߹੒

  17. ͳͥ5ඵʁ ͦͷલʹ࣮૷ํ๏ͷઆ໌

  18. socket.io(socket.io-swift) https://github.com/socketio/socket.io-client-swift

  19. socket.ioͱ͸ • WebSocketΛϥοϓͨ͠ϥΠϒϥϦ • αʔόʔαΠυ & ΫϥΠΞϯταΠυΛαϙʔτ • ࣮͸WebSocketͦͷ΋ͷΑΓ΋WebSocket͕࢖͑ͳ ͍؀ڥ΍઀ଓ͕੾Εͨ࣌ʹਅՁΛൃش͢Δ

    /PEF+4 +BWB /PEF+4 4XJGU +BWB $
  20. WebSocket͕ܨ͕Βͳ͍ཧ༝͸ ͍ͬͺ͍͋Δ ͦͷ࣌ʹsocket.io͸ ࣗಈͰHTTP Pollingʹ ੾Γସ͑ͯ͘ΕΔ ηΩϡϦςΟιϑτ proxy Firewall

  21. WebSocketͩͬͯ઀ଓ͸੾ΕΔ socket.io͸࠶઀ଓ΋ ࣗಈͰ΍ͬͯ͘ΕΔ ௨৴ෆྑ ௨৴ݍ֎

  22. iOSͷ࿥Իํ๏ • SkyWay SDKͷgetUserMedia
 -> Ұ୺຤ʹ͖ͭҰ͔ͭ͠औಘͰ͖ͳ͍
 -> Ի੠Stream͚ͩൈ͖ग़ͤͳ͍ • ϚΠΫͷԻ੠Λऔಘͯ͠ૹ৴


    -> ͜ͷํ๏Ͱ࿥Ի
  23. iOSͷsocket.ioͰ Streamܗࣜͷૹ৴͕Ͱ͖ͳ͍ • WebSocketͰૹ৴Ͱ͖ΔϑΝΠϧ͸
 ςΩετܗࣜ or όΠφϦܗࣜ • ϑΝΠϧΛҰ౓อଘ͠
 όΠφϦܗࣜͰૹ৴

    ฐࣾͰ ӡӦ͍ͯ͠Δ IT༻ޠࣙయ
  24. ಋೖϑΣʔζ

  25. ࣮૷͢Δ 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() }
  26. σʔλΛૹ৴͢Δ func emit(_ event: String, _ items: SocketData...) { try?

    client.emit( event, with: items.map( {try $0.socketRepresentation()} ) ) } -> ͜ΕͰσʔλૹ৴·Ͱ֬ೝͰ͖ͨ ૹΔ࣌ʹҰ౓ϑΝΠϧܗࣜʹม׵͢Δඞཁ͋Γ
  27. ͜͜·Ͱ͸؆୯ ͔͜͜Β͕஍ࠈ

  28. ͔͜͜Β͸ ൃੜͨ͠໰୊ͱ ͦͷղܾʹ͍ͭͯ࿩͠·͢

  29. ᶃ HTTP઀ଓʹࠩ͠୅ΘΔ

  30. WebSocket͕ܨ͕Βͳ͍ͱ
 HTTP Pollingʹ੾ΓସΘΔ ͦͷ࣌ʹsocket.io͸ ࣗಈͰHTTP Pollingʹ ੾Γସ͑ͯ͘ΕΔ ηΩϡϦςΟιϑτ proxy Firewall

  31. ͜ͷ஌ݟ͕ͳ͔ͬͨࠒ ௐࠪΛߦ͏ 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઀ଓ͢ΔڍಈΛൃݟͨ͠
  32. ֬ೝͨ݁͠Ռɾɾɾ ͦͷ࣌ʹsocket.io͸ ࣗಈͰHTTP Pollingʹ ੾Γସ͑ͯ͘ΕΔ ηΩϡϦςΟιϑτ proxy Firewall ߏஙͨ͠αʔόʔ͕ HTTP௨৴Λ

    ڐՄ͍ͯ͠ͳ͍ɾɾ
  33. 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ʹͯ͘͠ΕΔ
  34. ᶄ ίωΫγϣϯΦϒδΣΫτ͕ ෳ਺Ͱ͖Δ

  35. Ϩοεϯը໘ʹೖͬͨޙ ࠶ೖࣨͨ࣌͠ʹ ઀ଓ͕ಈ͔ͳ͍͜ͱ͕͋ͬͨ

  36. ௐ΂ͨͱ͜Ζ ΦϒδΣΫτΛഁغ͚ͨͩ͠Ͱ͸ ઀ଓ͸੾Εͳ͍

  37. disconnectͰඞͣ઀ଓΛด͡Δ func disconnect() { client.disconnect() } func isConnected() -> Bool

    { guard let client = client else { return false } return client.status == SocketIOClientStatus.connected } ઀ଓΛด͡Δ ઀ଓεςʔλεΛ͍࣋ͬͯΔͷͰ ઀ଓத͔֬ೝͰ͖Δ
  38. ᶅ 5ඵ͝ͱΛ࣮ݱ͢Δ

  39. TimerΫϥε͕ ࢖༻Ͱ͖·͢

  40. 5ඵ͝ͱͷαΠΫϧ ࿥Ի։࢝ͯ͠ 5ඵ଴ͭ ࿥Իఀࢭ͠ ϑΝΠϧૹ৴ λΠϚʔىಈ ϑΝΠϧΛ࡟আ

  41. scheduledTimerͰ 5ඵ͝ͱΛ࣮ݱ͢Δ Timer.scheduledTimer( timeInterval: 5, target: self, selector: #selector(self.chunkTimerEvent), userInfo:

    event, repeats: true )
  42. ͔͠͠ 5ඵ͝ͱʹ͸ͳΔ΋ͷͷ ͖ͬͪΓ5ඵʹ͸ͳΒͳ͍

  43. scheduledTimer͸ ϝΠϯεϨουͰಈ࡞͢ΔͨΊ UIͳͲϝΠϯεϨουͰڝ߹͢Δͱ ׂΓࠐΈ͕ೖͬͯಈ͔ͳ͘ͳΔ

  44. 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 )
  45. 5ඵ͝ͱʹͳΒͳ͍໰୊Λ ௐ΂͍ͯͨ࣌ɺTimerͰ͸ͳ͘ ૹ৴͕5ඵ͝ͱʹͳͬͯͳ͍ͷͰ͸ ͱௐࠪͨ͠

  46. emitΛϝΠϯεϨουͰಈ͘Α͏ʹ func emit(_ event: String, _ items: SocketData...) { DispatchQueue.main.async

    { try? client.emit( event, with: items.map( {try $0.socketRepresentation()} ) ) } } -> վળ͞Εͳ͔ͬͨ
  47. 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
  48. ղܾࣗମ͸TimerͩͬͨͷͰ͕͢ ॲཧॱ͕อূ͞ΕΔ͜ͱΛ஌Εͯ ྑ͍ษڧʹͳΓ·ͨ͠

  49. ·ͱΊ

  50. ·ͱΊ WebSocket is Easy WebSocket is Hard ͚ͩͲ

  51. ·ͱΊ WebSocket is Easy WebSocket is Hard ͚ͩͲ WebSocket΋ ۜͷ஄ؙͰ͸ͳ͍

  52. ϓϩμΫτʹಋೖ͢Δ࣌͸ ͔ͬ͠Γ४උͯ͠ τϥϒϧΛ૝ఆ͠·͠ΐ͏

  53. ͋Γ͕ͱ͏͍͟͝·ͨ͠