简体   繁体   English

Web Socket 连接 - iOS Swift 中的拒绝/失败连接 - Starscream/RxStarscream

[英]Web Socket Connection- refused/failed connection in iOS Swift - Starscream/RxStarscream

I have been facing Ann issue with Websocket to connection, I have tried several methods to get connected, but always showing connection refused or failed connection.我一直面临着 Websocket 连接的 Ann 问题,我尝试了几种方法来连接,但总是显示连接被拒绝或连接失败。 The library I have using is Starscream ref:- https://github.com/daltoniam/Starscream and also tried with RxStarscream ref:- https://github.com/RxSwiftCommunity/RxStarscream .我使用的库是Starscream ref:- https://github.com/daltoniam/Starscream并尝试使用RxStarscream ref:- https://github.com/RxSwiftCommunity/RxStarscream

In both cases when I tried the following code,在这两种情况下,当我尝试以下代码时,

@_exported import RxSwift
@_exported import RxStarscream
import Starscream

  override func viewDidLoad() {
        super.viewDidLoad()
 private let disposeBag = DisposeBag()
 
socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)
        socket.connect()

 socket.rx.response.subscribe(onNext: { (response: WebSocketEvent) in
            switch response {
            case .connected:
                print("Connected")
            case .disconnected(let error):
                print("Disconnected with optional error : \(error)")
            case .message(let msg):
                print("Message : \(msg)")
            case .data(_):
                print("Data")
            case .pong:
                print("Pong")
            }
        }).disposed(by: disposeBag)
}

I am beginner in web socket, what am trying to configure is I need to connect the websocket and socket event need to call, followed by target我是网络套接字的初学者,我尝试配置的是我需要连接网络套接字和需要调用的套接字事件,然后是目标

Socket Events
URI : ws://{host}:{port}?token={_token}&lang={en}
Ip = {dev,qc}.example-app.info
Port = 8090
Main structure for Events Request and Response
{
event:”Event Name”
data:”Event Data”,
is_player:boolean(true,false)
}
Example: {"event":"xxxx","data":"xxxx", "is_player":true}

If anyone could give an idea or just suggestion on how to implement on the above web socket method for iOS with swift will be appreciated.如果有人可以就如何使用 swift 在上述 iOS 网络套接字方法上提供想法或建议,将不胜感激。

Move your private let disposeBag = DisposeBag() to the outside of the viewDidLoad .将您的private let disposeBag = DisposeBag()viewDidLoad的外部。 Because the way you've done it, your subscription dies as soon as your viewDidLoad finishes executing.因为按照您的做法,一旦您的 viewDidLoad 完成执行,您的订阅就会终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM