Action Cable客户端未在iOS中收到频道onReceive的响应

时间:2019-07-02 05:27:21

标签: ios sockets swift4 actioncable xcode10.1

在我的项目中,我有一个要求,即用户将图像上传到服务器,并且上传的图像将在后端进行编辑,并且用户会收到新图像。由于图像编辑需要时间,因此我们将利用套接字连接来知道编辑一旦完成。

所以,我目前的方法是,一旦图像上传完成,我就在Action Cable客户端的帮助下创建了一个频道,当我从服务器获取完整的图像时,我应该在频道onReceive上得到响应。方法。

但是现在执行此操作时,我遇到两个问题,第一个主要问题是onReceive回调有时有效,有时却无效。 第二,我总是在回调中收到失败的响应。这不是服务器问题,因为我们在Android中也是如此,并且可以在那完美地工作。任何帮助表示赞赏。以下是我的ActionCableManager类。

class ActionCableManager {

    var client: ActionCableClient?
    var channel: Channel?
    let url = "wss://mysite.com/cable"
    let ChannelIdentifier = "MyChannel"

    init(){

        if self.client == nil{
            setupActionCableClient()
        }
    }


    func establishConnection() {
        guard let client = client else {
            return
        }
        client.connect()
    }

    func closeConnection() {
        self.client?.disconnect()
    }



    /*===============================================================================*/
    // MARK: - Setup connection
    /*===============================================================================*/

    func setupActionCableClient(){

        guard let urlString = URL(string: url) else {return}

        let headers = ["Origin":"Mysite address"]
        self.client = ActionCableClient(url: urlString, headers:headers)

        self.client?.willConnect = {
            print("Will Connect")
        }

        self.client?.onConnected = {
            print("Connected to \(String(describing: self.client?.url))")
        }

        self.client?.onDisconnected = {(error: ConnectionError?) in
            print("Disconnected with error: \(String(describing: error))")
        }

        self.client?.willReconnect = {
            print("Reconnecting to \(String(describing: self.client?.url))")
            return true
        }


    }

    func createChannel(_ roomId:String){

        let room_identifier = ["room_id" : roomId]
        self.channel = client?.create(ChannelIdentifier, identifier: room_identifier, autoSubscribe: true, bufferActions: true)

        self.channel?.onSubscribed = {
            print("Subscribed to \(self.ChannelIdentifier) with room Id=\(roomId)")
        }

        self.channel?.onReceive = {(data: Any?, error: Error?) in

            print("****** channel on receive data = \(String(describing: data))")
            if let error = error {
                print(error.localizedDescription)
                return
            }
        }

    }

}

这就是我在MainViewController上使用它的方式:

let actionCableManager = ActionCableManager()

 override func viewDidLoad() {
    super.viewDidLoad()

    actionCableManager.establishConnection()
  }


//To upload image to server and register Action cable channel
func uploadImage(){

  //Some code to upload the image

//check for image upload status
if let httpResponse = response as? HTTPURLResponse {

            //Image upload was successful
                if httpResponse.statusCode == 200{

                    DispatchQueue.main.async {

                        self.actionCableManager.createChannel(self.roomId)
                    }
                }
           }
}

下面是一段有效的Android代码:

// 1. Setup
URI uri = new URI("wss://mysite.com/cable");

Consumer.Options options=new Consumer.Options();
 options.headers = new ArrayMap<>();
 options.headers.put("Origin","My Site address");
 Consumer consumer = ActionCable.createConsumer(uri,options);

// 2. Create subscription
 Channel appearanceChannel = new Channel("MyChannel");
 appearanceChannel.addParam("room_id",randomString);
 Subscription subscription = consumer.getSubscriptions().create(appearanceChannel);


subscription
    .onConnected(new Subscription.ConnectedCallback() {
        @Override
        public void call() {
            // Called when the subscription has been successfully completed
        }
    }).onRejected(new Subscription.RejectedCallback() {
        @Override
        public void call() {
            // Called when the subscription is rejected by the server
        }
    }).onReceived(new Subscription.ReceivedCallback() {
        @Override
        public void call(JsonElement data) {
            **//Gets a success response here with transformed image URL**
        }
    }).onDisconnected(new Subscription.DisconnectedCallback() {
        @Override
        public void call() {
            // Called when the subscription has been closed
        }
    }).onFailed(new Subscription.FailedCallback() {
        @Override
        public void call(ActionCableException e) {
            // Called when the subscription encounters any error
        }
    });

// 3. Establish connection
consumer.connect();

PS:我正在将这个github库用于ActionCableClient

0 个答案:

没有答案